如何从弹出窗口中的下拉列表中选择值

时间:2015-11-04 12:30:58

标签: java selenium-webdriver

我点击链接然后弹出窗口,弹出窗口就是下拉菜单。 我的问题是如何从弹出窗口中的下拉列表中选择值。

下面是html:

<select onchange="formsubmit()" id="selectedchannel" name="selectedchannel">
                    <option value="521">Abec</option>
                    <option value="3322">Abdwdc</option>
                    <option value="1463">Abfc</option>
                    <option value="3849">Abdfc</option>
                    <option value="960">Abdfc</option>
                   </select>

1 个答案:

答案 0 :(得分:0)

首先你必须切换到帧然后才能选择值。

以这种方式选择特定值:

driver.switchTo().frame(0); // Here 0 is index of frame , you can switch to frame by it's name also. i.e  driver.switchTo().frame("your frame name");

Select option = new Select (driver.findElement(By.id("selectedchannel")));
option.selectByVisibleText("Abec");

您可以通过以下3个选项中的任何一个选择选项:

option.selectByVisibleText("") // it can be Abdfc,Abec etc.

option.selectByIndex(index) // it can be 0 ,1,2 etc

option.selectByValue(value); //it can be 521,960 etc.