Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click an <option> element with WebDriver?

This is a piece of UI code

<select id="order_unit_line_rate_806782_is_addenda_enabled" class="selects_for_487886" onchange="select_addendum(806782, this);dateShowMemory(this.options[this.selectedIndex].value, '806782');" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / drop down" name="order_unit_line_rate[806782][is_addenda_enabled]">
     <option value="0" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / Fee">                     Fee                   </option>
     <option value="1" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Additional Usage Commitment / See Attached Addendum">                     See Attached Addendum                   </option>
</select>

where the <option> tags are nested inside the <select> tag. I need to click() on the second <option> element which is an item in the dropdown list. The dropdown is clickable when i try to click() on the <select> tag using id / uniqueattr.

How do I traverse the <option> tags nested under <select> and click on the right item?

like image 604
Kiran Koundinya Avatar asked Jul 05 '12 11:07

Kiran Koundinya


2 Answers

Besides the correct Qwerky's answer, you can also do simple

driver.findElement(By.xpath("//select/option[@value='1']")).click();

This finds the option element with value='1' and clicks it, practically selecting it in the drop-down.

Both mine and Qwerky's solution are described and explained here, in the documentation.

like image 111
Petr Janeček Avatar answered Sep 19 '22 14:09

Petr Janeček


This will select the option with value "1" in the select with id "order_unit_line_rate_806782_is_addenda_enabled".

Select select = (Select)webdriver.findElement(By.id("your id here"));
select.selectByValue("1");

You can also select by index or text; see the docs.

like image 20
Qwerky Avatar answered Sep 18 '22 14:09

Qwerky