2016-12-28 114 views
0

想从下拉列表,其中的元素没有索引或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 here is the html code

+0

显示您使用的确切选择器 – Andersson

+2

可能的重复[如何在使用Jav的Selenium WebDriver中选择下拉值A](http://stackoverflow.com/questions/20138761/how-to-select-a-dropdown-value-in-selenium-webdriver-using-java) – BackSlash

回答

0

尽量用简单click()方法来选择所需的下拉选项:

_wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(), 'Egypt (+20)')]"))).click(); 

XPath"//a[@country='Egypt']""//a[@value='EG']"似乎也适用

请注意,您需要点击相应的元素打开下拉上在点击它的选项之前下调

0

的截图如果你指的HTML你可以看到Dropdown使用HTML Select标签不建。所以你不能使用下面的方法: -

  1. selectByValue
  2. selectByIndex
  3. selectByVisibleText

您必须确定使用常规的方法来处理下拉列表中的元素。例如:尝试通过标识元素来点击下拉菜单,然后再次点击国家/地区名称以选择值。

相关问题