user6696575
user6696575

Reputation: 43

Selenium with Java - Unable to create a loop for a drop down

I could not manage to select the next drop down item through the locator of the selected item. My intention is to test a site for different languages through the drop down. Sending keyboard down arrow activates the scroll bar in the drop down. Could anyone please help on this ?

driver.findElement(By.xpath(".//*[@id='trigger']/div/paper-input/paper-input-container")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath(".//*[@id='langList']//paper-item//.[@tabindex=\"0\"]")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath(".//*[@id='langList']//paper-item//.[@tabindex=\"0\"]")).sendKeys(Keys.ENTER, Keys.ARROW_DOWN);

Upvotes: 3

Views: 117

Answers (2)

user6696575
user6696575

Reputation: 43

Thanks a lot. It works now with the following code:

driver.findElement(By.xpath(".//*[@id='trigger']/div/paper-input/paper-input-container")).click();
    Thread.sleep(1000);
    driver.findElement(By.cssSelector(".style-scope.making-language-selector.iron-selected.x-scope.paper-item-0")).click();

    driver.findElement(By.cssSelector(".style-scope.making-language-selector.iron-selected.x-scope.paper-item-0")).sendKeys(Keys.ARROW_DOWN, Keys.ENTER);

Upvotes: 1

Cromzinc
Cromzinc

Reputation: 102

I was going to write out some examples, but remembered Dave Haeffner had already covered this in his elemental selenium series.

You can find the great write-up of that here: How To Select from a Dropdown in Selenium

I will mention one thing though. You should make a great effort at never using implicit waits (Thread.sleep()). They do not make for clear exception errors unless they are handled well, and will slow down your tests greatly. Identify what you are waiting on, and create an explicit wait. If you need more information on creating those, I can go into more detail.

Upvotes: 2

Related Questions