如何在Selenium 2中选择/获取下拉选项 [英] How to select/get drop down option in Selenium 2
本文介绍了如何在Selenium 2中选择/获取下拉选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将硒1代码转换为硒2,在下拉菜单中找不到任何简单的方法来选择一个标签,或者选择一个下拉菜单。您是否知道如何在Selenium 2中执行此操作?
I am converting my selenium 1 code to selenium 2 and can't find any easy way to select a label in a drop down menu or get the selected value of a drop down. Do you know how to do that in Selenium 2?
这里有两个在Selenium 1中可用的语句,但不在2中:
Here are two statements that work in Selenium 1 but not in 2:
browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");
推荐答案
查看有关填写表单,在selenium文档中使用webdriver,对于选择课程。
Take a look at the section about filling in forms using webdriver in the selenium documentation and the javadoc for the Select class.
根据标签选择一个选项:
To select an option based on the label:
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");
获取第一个选定的值:
WebElement option = select.getFirstSelectedOption()
这篇关于如何在Selenium 2中选择/获取下拉选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文