本文介绍了如何使用Selenium WebDriver(Selenium 2)从组合框中获取选定的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下html代码:
Assume I have this html code:
<select id="superior" size="1" name="superior">
<option value=""></option>
<option value="c.i.e.m.md.Division_1">DIVISION007</option>
<option selected="selected" value="c.i.e.m.md.Division_$$_javassist_162_119">MyDivision</option>
<option value="c.i.e.m.md.Division_121">MyDivision4</option>
<option value="c.i.e.m.md.Division_122">MyDivision5</option>
</select>
这是一个带有
id=superior
,并选择了当前值MyDivision.
and currently value MyDivision is selected.
使用Selenium WebDriver,我试图获取选定的值,但没有成功.
Using Selenium WebDriver I am trying to get the selected value, but no success.
我尝试过:
String option = this.ebtamTester.firefox.findElement(By.id(superiorId)).getText();
return option;
但这会返回我组合框中的所有值.
But this returns me all the values in the combobox.
请帮助?
WebElement comboBox = ebtamTester.firefox.findElement(By.id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
String wantedText = selectedValue.getValue();
推荐答案
这是用C#编写的,但不难将其转换为您使用的任何其他语言:
This is written in C#, but it shouldn't be hard to transition it over to any other language you're using:
IWebElement comboBox = driver.FindElement(By.Id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
string wantedText = selectedValue.SelectedOption.Text;
SelectElement要求您使用OpenQA.Selenium.Support.UI,因此在顶部键入
SelectElement requires you to use OpenQA.Selenium.Support.UI, so at the top, type
using OpenQA.Selenium.Support.UI;
我想为您服务,而不是使用驱动程序"
I suppose for you, instead of 'driver' you would use
IWebElement comboBox = this.ebtamTester.firefox.FindElement(By.Id("superior"));
这篇关于如何使用Selenium WebDriver(Selenium 2)从组合框中获取选定的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!