2

Scenario: To search and select the first destination location that matches the input string using the below code.

Code snippet:

driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.manage().window().maximize();

driver.get("https://www.emirates.com/nz");

Thread.sleep(2000L);;

WebElement Depfield = driver.findElement(By.xpath("//div[contains(@class,'active')]//*[@name='Departure airport']"));

Depfield.click();

Depfield.sendKeys("lag");

Thread.sleep(2000L);;

Depfield.sendKeys(Keys.ARROW_DOWN);

Depfield.sendKeys(Keys.RETURN); }

Issue: The instruction Depfield.sendKeys(Keys.ARROW_DOWN) is actually moving the cursor to the bottom of the search drop down list instead of the first matched option. As a result, no option is being selected by the following instruction - Depfield.sendKeys(Keys.RETURN). I have tried with for loop to go one option after the other but it didn't work.

enter image description here

New Update: New result

enter image description here

Request: Can anyone help me in resolving this issue?

  • it works for me ,it selecting Lagos nigeria – PDHide Apr 10 at 7:55
  • Hi Friend. Thank you for the response. I have run the code multiple times but it never selected Lagos Nigeria. Am I missing anything here? – SelfLearner Apr 10 at 8:10
  • I have attached the screenshot of my latest results. – SelfLearner Apr 10 at 8:12
  • sorry my mistake, ya you are right – PDHide Apr 10 at 8:28
1

The below code works:

         Depfield.sendKeys("lag");

         Thread.sleep(2000L);;

         Depfield.sendKeys(Keys.ARROW_DOWN,Keys.RETURN);

I think the keypress is not released until another keypress is provided, so for clicking second element use:

Depfield.sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.RETURN);

You can use actions class also:

       Depfield.sendKeys("lag");

         Thread.sleep(2000L);;

        Actions builder = new Actions(driver);
        Action drawAction = builder.sendKeys(Keys.ARROW_DOWN)
                .sendKeys(Keys.ARROW_DOWN)
                .sendKeys(Keys.ARROW_DOWN)
                .sendKeys(Keys.ARROW_DOWN)
                .sendKeys(Keys.ENTER)
                  .build();
        drawAction.perform();
| improve this answer | |
  • You are a genius, my friend! It has worked now with your code. What a big sigh of relief. Thanks a lot for sharing your knowledge. Sure, I will try with actions class too. – SelfLearner Apr 10 at 8:58
  • Could you upvote also, by clicking the up arrow near to my answer – PDHide Apr 10 at 8:59
  • May I ask you to explain in detail (if possible) why my code didn't work? Am I not supposed to use the keys like that? – SelfLearner Apr 10 at 9:00
  • As i mentioned, I think the keypress is not released until another keypress is provided, I saw the focus going all the way to the end of the drop-down. I am not much sure – PDHide Apr 10 at 9:01
  • 1
    No problem friend. I have upvoted this answer and yesterday's answer too. – SelfLearner Apr 10 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.