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">
<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="SM">Social Media</option>
<option value="AS">Antique Shop </option>
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"))); dropDown.selectByIndex(int);
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"))); dropDown.selectByIndex(1);