Skip to main content
  1. About
  2. For Teams
Asked
Modified 7 days ago
Viewed 56 times
0

I'm trying to test a travel website. When I enter the city code, the correct airport should be picked from the list. However, it enters the city code and does not proceed to pick up the city from the list.

    @org.testng.annotations.Test    
    public void flightTest() throws InterruptedException {
        
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        driver.get("https://phptravels.net/");
        
        //--------------- Departure -------------------
        
        WebElement fromBox = driver.findElement(By.name("from"));
        fromBox.sendKeys("DAC");
       
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        By lst1 = By.xpath("//div[@class='most--popular-from fadein results-container-from rounded-2 shadow border-0']//button[text()='DAC']");
        Thread.sleep(1000);
        
        WebElement from = wait.until(ExpectedConditions.visibilityOfElementLocated(lst1));
        Thread.sleep(1000);
        from.click();
        
        
        //--------------- Destination -------------------
        
        WebElement toBox = driver.findElement(By.name("to"));
        toBox.sendKeys("JED");
       
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        By lst2 = By.xpath("//div[@class='d-flex align-items-center p-2 to--insert overflow-hidden result-option']//button[text()='JED']");
        
        WebElement to = wait.until(ExpectedConditions.visibilityOfElementLocated(lst2));
        Thread.sleep(1000);
        to.click();

    }
2
  • 1
    Not an answer, but don't mix implicit and explicit waits. You should remove the implicit wait and sleeps.
    Corey Goldberg
    –  Corey Goldberg
    2025-10-10 12:45:20 +00:00
    Commented Oct 10 at 12:45
  • @Corey Goldberg Thanks. I will remove it and try
    amal
    –  amal
    2025-10-10 13:02:38 +00:00
    Commented Oct 10 at 13:02

1 Answer 1

2

It's not the issue in your code. It's just the way website behaves. i.e. if you enter(in this case selenium) the airport code(JED) too fast, system won't auto suggest just one matching airport, it suggests random but many airports, this is where your code fails. I don't know if this is a system bug, but that's not important to us.

You need to handle this. i.e. make selenium type the keys slowly as if a human is typing. I have written a method to enter the keys slowly. See the working code below:

public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new ChromeDriver();
    driver.get("https://phptravels.net/");
    driver.manage().window().maximize();
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

    //--------------- Departure -------------------
    WebElement fromField = wait.until(ExpectedConditions.elementToBeClickable(By.name("from")));
    typeSlowly(fromField,"DAC",500);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='DAC']"))).click();

    //--------------- Destination -------------------
    WebElement toField = wait.until(ExpectedConditions.elementToBeClickable(By.name("to")));
    typeSlowly(toField,"JED",500);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='JED']"))).click();

    //--------------- Departure Date -------------------
    wait.until(ExpectedConditions.elementToBeClickable(By.id("departure"))).click();
}

public static void typeSlowly(WebElement element, String text, int delayMillis) {
    for (char ch : text.toCharArray()) {
        element.sendKeys(Character.toString(ch));
        try {
            Thread.sleep(delayMillis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so so much for the explanation and the code. It works. I tried different ways for 2 days, and I guess this is the only way to fix it due to the site's behavior. Thanks again :)

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.