This is how I send "Amsterdam" into the field. But it shows no result. I also tried to .clear() the field.
WebElement flyToSearch = webDriver.findElement(By.xpath("//input[contains(@class, 'select2-search__field')]"));
flyToSearch.sendKeys("Amsterdam");
flyToSearch.sendKeys(Keys.ENTER);
But if I type it by hand. It returns the result. I did the same thing for the search field right next to it. It worked fine.
Here below I close the log in pop up first. Then box for one-way flight tickets is selected. Then I set the origin to Istanbul. But I can not set destination to Amsterdam or any other city.
private static final WebDriver webDriver = WebDriverReady.getWebClient();
public static void main(String[] args) {
try {
openPage();
closeLogin();
selectOneWay();
fillFlyFrom();
fillFlyTo();
} catch (Exception e) {
e.printStackTrace();
webDriver.close();
}
}
private static void openPage() {
webDriver.get(PegasusConstants.URL);
}
private static void closeLogin() {
webDriver.findElement(By.className("nxm2_bolbol-login_header")).click();
webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
private static void selectOneWay() {
webDriver.findElement(By.xpath("//label[contains(@for, 'one_way')]")).click();
webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
private static void fillFlyFrom() {
WebElement flyFrom = webDriver.findElement(By.xpath("//label[contains(@for, 'fs_from')]"));
WebElement parentOfFlyFrom = (WebElement) ((JavascriptExecutor) webDriver).executeScript(
"return arguments[0].parentNode;", flyFrom);
parentOfFlyFrom.click();
webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
WebElement flyFromSearch = webDriver.findElement(By.xpath("//input[contains(@class, 'select2-search__field')]"));
flyFromSearch.sendKeys("Istanbul");
flyFromSearch.sendKeys(Keys.ENTER);
webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
private static void fillFlyTo() {
WebDriverWait wait = new WebDriverWait(webDriver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("nxm2CookieSubmitButton"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='From']/preceding-sibling::span/descendant::span[@role='presentation']"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.select2-search__field"))).sendKeys("Amsterdam");
List<WebElement> sourceOptions = webDriver.findElements(By.cssSelector("ul li[role='treeitem']"));
for (WebElement ele : sourceOptions) {
ele.click();
}
// WebElement flyTo = webDriver.findElement(By.xpath("//*[text()='Nereye']"));
// WebElement parentOfFlyTo = (WebElement) ((JavascriptExecutor) webDriver).executeScript(
// "return arguments[0].parentNode;", flyTo);
// parentOfFlyTo.click();
//
// webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//
// WebElement flyToSearch =
// new WebDriverWait(webDriver, 20)
// .until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@class, 'select2-search__field')]")));
// flyToSearch.sendKeys("Amsterdam");
// flyToSearch.sendKeys(Keys.ENTER);
//
// webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
What could be the problem here? Thanks
CodePudding user response:
This could be cause element is not rendered properly and you are trying to interact with it. Please try with Explicit waits :
WebElement flyToSearch = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@class, 'select2-search__field')]")));
flyToSearch.sendKeys("Amsterdam");
flyToSearch.sendKeys(Keys.ENTER);
or with JS :
WebElement flyToSearch = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@class, 'select2-search__field')]")));
(JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'Amsterdam')", flyToSearch)
Udpate 1 :
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.flypgs.com/en");
wait.until(ExpectedConditions.elementToBeClickable(By.id("nxm2CookieSubmitButton"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='From']/preceding-sibling::span/descendant::span[@role='presentation']"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.select2-search__field"))).sendKeys("Amsterdam");
List<WebElement> sourceOptions = driver.findElements(By.cssSelector("ul li[role='treeitem']"));
for (WebElement ele : sourceOptions) {
ele.click();
}
CodePudding user response:
Try with this code once.
Click on the to
field, send Amsterdam
and then select Amsterdam
from the list.
driver.get("https://www.flypgs.com/en");
//Select "One-way"
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@for='one_way']"))).click();
//Select "From"
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form[@id='fligth-searh']/div[2]/div[1]/div/span/span[1]/span"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Istanbul-S.Gokcen']"))).click();
//Select "to"
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form[@id='fligth-searh']/div[2]/div[2]/div/span/span[1]/span"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='select2-search__field']"))).sendKeys("Amsterdam");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Amsterdam']"))).click();
//Click on "search"
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='SEARCH CHEAP FLIGHTS']"))).click();
//Click on "I Agree"
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='I Agree']"))).click();