2016-12-28 48 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

您必須確定使用常規的方法來處理下拉列表中的元素。例如:嘗試通過標識元素來點擊下拉菜單,然後再次點擊國家/地區名稱以選擇值。

相關問題