2

I want to select an item in a CSS dropdown but there seems to be no way to do it, we can take as an example the google hotel review page here how can I select by most recent reviews programmatically through selenium ?

Basically I want to see all the reviews of the hotel sorted by Most recent instead of most useful, but since by default are ordered by most useful I need to switch through the dropdown programmatically.

I've tried this way :

        Select select = new Select(driver.findElement(By.xpath("//*[@id=\"gsr\"]/g-lightbox/div[2]/div[3]/div/div/div/div[1]/div[3]/div[2]/g-dropdown-menu/g-popup/div[2]/g-menu")));
        select.deselectByIndex(1);

but I'm getting an exception(org.openqa.selenium.support.ui.UnexpectedTagNameException) saying :

Element should have been "select" but was "g-dropdown-menu"

Is there a way to simulate a click on a CSS dropdown element like this with Selenium web driver ?

2
  • I am not sure if I understood your question correctly. I was able to trace down the xpath till //g-dropdown-menu/g-popup but not beyond that. Can you update us the manual step please? Commented Sep 14, 2017 at 16:35
  • Basically I want to see all the reviews of the hotel sorted by Most recent instead of most useful, but since by default are ordered by most useful I need to switch through the dropdown programmatically. What I need to do is select on the dropdown most recent using selenium . Any Ideas ? Commented Sep 14, 2017 at 16:47

1 Answer 1

2

Analysis:

Selenium Java API: Select.class only suitable for dropdown which use HTML select tag. For dropdown implment by other way, like JQuery dropdown plugin, Select class not support, for such dropdown you need to click on the drop down to make the options to display out, then choose the option you wanted.

Solution:

public void selectSortby(String sortBy) {
  // click on dropdown to expand options
  driver.findElement(By.xpath("//div[span[text()='Sort by:']]//g-dropdown-button").click();
  // choose option
  driver.findElement(By.xpath("//g-menu-item/div[text()='"+sortBy+"']")).click();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.