How to click an <option> element using WebDriver?

This is part of the user interface 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> . I need click() on the second <option> element, which is an element in the drop-down list. The dropdown menu can be clicked when I try to click() on the <select> tag using id / uniqueattr.

How can I navigate the <option> tags nested in <select> and click on the desired element?

+6
source share
2 answers

This will select the option with a value of "1" in the list with the identifier "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 documents .

+4
source

Besides Qwerky's correct answer, you can also make simple

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

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

It describes both mine and Qwerky's solutions, and here, in the documentation .

+7
source

Source: https://habr.com/ru/post/919745/


All Articles