Akash Dugam Akash Dugam - 2 years ago 58
HTML Question

How do I select element which are dynamically generating from drop down menu using selenium?

Sometimes in drop down list, you need to select content which is generating dynamically. Let's take an example, In drop down search box, if I enter 'Social' then drop down option like 'Social Media' is generated. I want to select that generated content. How do I do that?

HTML code look like:

<div id="xyz" class="togglebox" style="display:none">
<div class="formItem">
<label >What is your business?</label>
<div class="input"><select name="abc" class="validate" id="qwerty"><option value="" selected="selected">Please select...</option>
<option value="AC1">Accountant</option>
<option value="SM">Social Media</option>
<option value="AS">Antique Shop </option>

I want to select my business as Antique shop. How do I do that? Each time I select it by value it throws the error 'Element is not visible'

Answer Source

Here you can use the method called 'selectByIndex' Here is how to do it.

Select dropDown = new Select(driver.findElement(By.id("id of the dropDown")));

Here pass integer as the index. The first value in the drop down is at index 0, then next value is at index 1 so on. So if you want to select the 2 nd value what you have to do is this.

    Select dropDown = new Select(driver.findElement(By.id("id of the dropDown")));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download