I'm trying to select Chicago, IL from the Search City or Zip Code search box on https://weather.com/
Here's the code I've tried
driver.get("https://weather.com/");
driver.manage().window().maximize();
WebElement from = driver.findElement(By.id("LocationSearch_input"));
from.sendKeys("Chicago, IL");
Thread.sleep(500);
from.sendKeys(Keys.ARROW_DOWN);
from.sendKeys(Keys.ENTER);`
Any Help is appreciated. Thank you.
Actually it's better to wait for autosuggestions to be present (they are loaded with delay) and to click on element that contains your text input.
Like:
driver.get("https://weather.com/");
driver.manage().window().maximize();
WebDriverWait wdwait = new WebDriverWait(driver, 10);
By inputLocator = new By.ById("LocationSearch_input");
wdwait.until(ExpectedConditions.elementToBeClickable(inputLocator));
WebElement from = driver.findElement(inputLocator);
from.click();
String city = "Chicago, IL";
from.sendKeys(city);
String cityLocator = String.format("//*[@data-testid='ctaButton' and contains(.,'%s')]", city);
wdwait.until(ExpectedConditions.presenceOfElementLocated(new By.ByXPath(cityLocator)));
driver.findElement(new By.ByXPath(cityLocator)).click();
Try this code, it works for me.