Currently, I am in the process of developing a test using Selenium Webdriver. My goal is to automatically select the second option from every dropdown menu that may appear on the page. It's worth noting that the number of dropdown menus will vary with each test run.
I have devised the following script, but unfortunately, it doesn't seem to be functioning as intended:
if (driver.findElements({tagName: 'select'})) {
var select = driver.findElements({tagName: 'select'});
for (i = 0; i < select.length; i ++) {
i++;
driver.findElement(webdriver.By.xpath('//select['+i+']/option[2]')).click();
}
}
Here is an example of the HTML structure:
<select class="form-control" name="answer_4282670">
<option value="0">Please choose one...</option>
<option value="option a">option a</option>
<option value="option b">option b</option>
<option value="option c">option c</option>
<option value="Other" data-other-flag="">Other</option>
</select>
Since the value of each option differs across instances, selecting by value is not feasible.
Do you have any suggestions on how to rectify this issue so that the second option of every dropdown menu can be clicked, should one appear?