【问题标题】:check which item is selected from a list using selenium webdriver使用 selenium webdriver 检查从列表中选择的项目
【发布时间】:2015-06-22 21:22:27
【问题描述】:
我有一个select html 项目。
<select name="badge" data-validate="true"><option value="">none</option><option value="usd">USD</option>
<option value="eur">EURO</option>
<option value="ils">GBP</option></select>
我想用 selenium webdriver 来
(1) 从列表中选择一个项目EURO
(2)检查EURO从列表中是否被选中
我该怎么做?
【问题讨论】:
标签:
java
select
selenium
selenium-webdriver
jquery-selectors
【解决方案1】:
您可能想使用Select 类。一个 C# 等效示例如下所示。应该不会太难
// select the first operator using "select by value"
IWebElement element = Driver.FindElement(By.Name("badge"));
SelectElement selectByValue = new SelectElement(element);
selectByValue.SelectByValue("EURO");
if (selectByValue.SelectedOption.Text.Contains("EURO "))
{
//pass
}
【解决方案3】:
public IconImageTag getSelectedIconImageTag() {
Select select = getIconImageTagsDropDown();
WebElement selected = select.getFirstSelectedOption();
return IconImageTag.valueOf(selected.getText());
}
private Select getIconImageTagsDropDown() {
By dropDownSelector = By.cssSelector("#display_body > div.form-group.image_uploader.offer_logo > label > " +
"select");
WebElement tagsDropDown = driver.findElement(dropDownSelector);
return new Select(tagsDropDown);
}
public void setSelectedIconImageTag(IconImageTag iconImageTag) {
Select select = getIconImageTagsDropDown();
select.selectByVisibleText(iconImageTag.toString());
}