1

I am practicing against the following website https://www.easyjet.com/en

I am passing a value of "London" into the Origin search box. This returns six airport matches. I'm then trying to trawl through the results and select the one that contains the word "Luton".

My code so far is:

package d_practise;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class easyjetMenu {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "C:\\Work\\Drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.easyjet.com/");

        Thread.sleep(2000);
        WebElement d = driver.findElement(By.cssSelector("input[name='origin']"));
        d.click();
        d.sendKeys("London");

        while(!d.getText().contains("Luton")) {
            d.sendKeys(Keys.DOWN);
        }
        if(d.getText().contains("Luton")) {
            d.sendKeys(Keys.ENTER);
        }
    }
}

This just continuously loops and no match is found. I have tried various phrases but no joy.

Anyone please able to help?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

1 Answers1

0

On passing the value of London into the Origin search box, to click on the autocomplete/autosuggestion with text as Luton you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and then click() on the desired element using the following Locator Strategy:

  • Code Block:

    import java.util.Collections;
    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class easyjet_com_origin {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.easyjet.com/en/");
            WebElement d = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='origin']")));
            d.click();
            d.sendKeys("London");
            //List<WebElement> origins = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@id='ui-id-1']//li/a/span")));
            List<WebElement> origins = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul#ui-id-1 li>a>span")));
            for(WebElement origin:origins)
            {
                if(origin.getText().contains("Luton"))
                    origin.click();
            }
        }
    }
    
  • Browser Snapshot:

Luton

DebanjanB
  • 118,661
  • 30
  • 168
  • 217