【问题标题】:How to get the text of the selected option using Selenium 2 PHPUnit如何使用 Selenium 2 PHPUnit 获取所选选项的文本
【发布时间】:2015-10-11 17:25:05
【问题描述】:
我试图在我的 Selenium 程序中获取文本“高光纸贴纸”,但我的程序所能做的就是获取所有选项的文本。任何可能帮助我完成此任务的建议或语法将不胜感激。
<select id="attr1" class="op_select" name="attr1">
<option value="16506">Matte Paper Sticker</option>
<option selected="selected" value="13187">High Gloss Paper Sticker</option>
<option value="16507">High Gloss Vinyl Sticker</option>
</select>
【问题讨论】:
标签:
html
selenium
selenium-webdriver
phpunit
【解决方案1】:
学习Xpath,你会没事的。
public IWebElement Attr1 {
get {
return this.browser.FindElement(By.XPath(@"//select[@id='attr1']/option[@selected='selected']")
}
}
【解决方案2】:
id 是查找元素的最快方式。
这只是一个例子
$id_select_box = $this->webDriver->findElement(\WebDriverBy::id('select_box'));
$selected_option = new \WebDriverSelect($id_select_box);
$this->assertEquals("String", $selected_option->getFirstSelectedOption()->getText());