Parag Mahajan
Parag Mahajan

Reputation: 15

How to select a combobox value with Selenium WebDriver where it's a div with role of combobox

My HTML code has a div tag with the role as combobox, i.e.

<div role="combobox">...</div>

I am trying to select an item from the combo box through selenium driver with java.

I tried using "Select" class as recommended here : How to select a dropdown value in Selenium WebDriver using Java

but since it is a div, I get an error saying that

"UnexpectedTagNameException: element should have been select but was div"

I think it is because of the div role="combobox".

Is there any way to resolve this issue ?

Upvotes: 1

Views: 22679

Answers (3)

Parag Mahajan
Parag Mahajan

Reputation: 15

I was able to solve this by first clicking on the div , which displayed all the options and then clicking on the required option.

Thank you to all of you for your suggestions.

Upvotes: 0

noor
noor

Reputation: 3004

As there is no

select html tag

in your html code,

"Select" class will not work here.

So u can do this in two ways(As u don't give ur details html code)

First process:

Step one: click on that combo box.

Step two: After click on combo box, combo box options will be shown with their link text or id or other locators.

for this, use this code:

driver.findElement(By.id("search_key.combobox")).click();//click on that combo

than

driver.findElement(By.linkText("ur combo option link text"));//click on ur desired combo option
or
driver.findElement(By.cssSelector("ur combo option's css path"));//u can use any other locator what is shown in ur html code after clicking on combo box

But after click on combo box, if combo options is not shown with any locator in inspect section, than use this code:

driver.findElement(By.id("search_key.combobox")).click();//click on that combo
for(int i = 0; i <= position; i++){
    Actions actions = new Actions(driver);
    actions.sendKeys(Keys.DOWN).build().perform();//press down arrow key
    Actions actions = new Actions(driver);
    actions.sendKeys(Keys.ENTER).build().perform();//press enter
}

//here "position" is , ur desired combo box option position,
//for ex. u want to choose 3rd option,so ur "position" will be 3.

Upvotes: 2

murali selenium
murali selenium

Reputation: 3927

Did you tried with Sendkeys()?

driver.findElement(By.xpath("//div[@role='combox']")).sendKeys("text to select exp: selenium");

if above does not works as expected, you can try with click on drop down and click on required option in that drop down.

Thanks

Upvotes: 1

Related Questions