Vinod
Vinod

Reputation: 21

How to select values in drop down using selenium selenium.select("","")?

I am doing automation using Selenium. I am using DefaultSelenium class, in our application I have a drop down. I want to get a value from this drop down.

Initially, I have scripted with selenium IDE, it gave me the code as:

selenium.select("id=skuOptionSIZE1c4b403", "label=8");

but when i start writing in code (Java), Eclipse throws an error while I am still able to see the drop down id present on the page:

Exception in thread "main" com.thoughtworks.selenium.SeleniumException: ERROR: Element id=skuOptionSIZE1cd7bfd not found

Can any one please help me how to get the values from drop down?

Upvotes: 2

Views: 19504

Answers (3)

Petr Janeček
Petr Janeček

Reputation: 38414

If you use IE8 or greater, press F12 and use the Developer Tools there. Particularly helpful should be the cursor icon, i.e. Select Element By Click which will allow you to select any element and see all the attributes assigned to it.

If you use Firefox 11, there is a similar tool under Web Developer menu. Or use the Firebug addon which is stronger, but more complex.

But! The main problem you'll have that the id will change from time to time. It seems to be generated automatically. That means you'll have to use some other way to select the element. You can use for example selenium.select("id=skuOptionSIZE*", "label=8");, or find it by XPath or css selector.

Upvotes: 0

CBRRacer
CBRRacer

Reputation: 4659

If you download the Webdriver Support dll then you can use the following

SelectElement select = new SelectElement(element);
select.SelectByIndex(8); //Where the number 8 is the base 0 index of the options

So if you have 10 options (0-9) SelectByIndex(8) will return the ninth option.

Upvotes: 0

Tarken
Tarken

Reputation: 2202

If you are using Selenium 2 aka Webdriver I d do it like this:

Select select = new Select(driver.findElemetn(/*Way to your drop down*/));
select.selectByValue("your value") 
//or
select.selectByVisibleText("your Test");

//alternativly you can do something like this
List<WebElement> options = select.getOptions();
//find your desired option
select.selectByVisibleText(option.getText());

Hope that helps.

Upvotes: 5

Related Questions