想从下拉列表,其中的元素没有索引或ID我只能通过价值选择选择国家代码,我尝试使用SelectByValue & VisibleText两个没有工作也试图列表元素和环路上他们,但没有工作或者如何使用java&selenium从下拉列表中选择元素值?
更新: 它给我的错误:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been “select” but was “button”
我怎样才能从元素的列表按钮选择?
这里是代码:
public void selectInDropDownMenuCountryCode(final WebDriver driver, final By selector, final String selection) {
_wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
final Select select = new Select(driver.findElement(selector));
//select.selectByVisibleText(selection);
//select.selectByValue(selection);
String code;
List<WebElement> optionsD = select.getOptions();
for (WebElement option : optionsD) {
code = option.getAttribute("value");
if (code == selection) {
option.click();
break;
}
}
}
HTML
显示您使用的确切选择器 – Andersson
可能的重复[如何在使用Jav的Selenium WebDriver中选择下拉值A](http://stackoverflow.com/questions/20138761/how-to-select-a-dropdown-value-in-selenium-webdriver-using-java) – BackSlash