1

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 ?

Lam Le
  • 1,489
  • 3
  • 14
  • 31
Parag Mahajan
  • 15
  • 1
  • 1
  • 4
  • You can only use `Select` class if the dropdown is built using `select html tag` else you need to use other methods to handle the dropdown. Can I have your HTML Snippet and your code that you have tried. – Paras Dec 28 '15 at 09:18
  • The html is :
    The code that I tried is : Select dropdown = new Select(driver.findElement(By.id("search_key.combobox")));
    – Parag Mahajan Dec 28 '15 at 10:08
  • pls post html for the combobox – parishodak Dec 28 '15 at 10:37

3 Answers3

2

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.
noor
  • 2,954
  • 2
  • 16
  • 29
1

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

murali selenium
  • 3,847
  • 2
  • 11
  • 20
  • The html is :
    The code that I tried is : Select dropdown = new Select(driver.findElement(By.id("search_key.combobox"))); I tried with SendKeys as well , but it didn't work.
    – Parag Mahajan Dec 28 '15 at 10:08
  • @ParagMahajan, it looks like normal div as there is no option in provided html. so please try like click on that div, then option will display in dropdown, then use click again on required option. – murali selenium Dec 28 '15 at 10:11
0

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.

Parag Mahajan
  • 15
  • 1
  • 1
  • 4