2017-08-09 140 views
2

enter image description here我想自动使用使用Java硒的网络驱动器的下拉菜单,但HTML页面有<option disabledselected>----</option>(参考附截图)无法选择下拉菜单

我想从下拉菜单中选择第二个菜单项下。我尝试了很多东西,但每次我收到错误消息。

一号方法 - 使用ByVisibleText

public void selectHomeCommunity(String name){ 
    Select hmecomm= new Select(hmecommdropdown); 
    hmecomm.selectByVisibleText(name); 
} 

public <Webelement> SelfRegistrationPage Community(String pass) { 
    // TODO Auto-generated method stub 
    enterPassKey(pass); 
    System.out.println("Entered into Community method"); 
    pressGoBtn(); 
} 

第二个方法 - JavascriptExecutor

((JavascriptExecutor)driver).executeScript("document.getElementById('hmecommdropdown').options.item(0).click().;"); 

第三届方法 - getFirstSelectedOption

String selectedLabel = new Select(driver.findElement(By.id("CommunityDropdown"))).getFirstSelectedOption().getText(); 

每当我得到同样的错误:

等待的能见度[[ChromeDriver:在XP (9a6751455dba60b65479430ff8f9aa00)铬] - > ID:CommunityDropdown]

+0

分享请相关的HTML。 – DebanjanB

+1

哪里截图..? –

+0

请分享html DOM /网站网址。 –

回答

0

如果下拉菜单是从一种语言如角,然后使用选择可能不起作用。我的建议是我们硒的点击操作。

  1. 点击下拉菜单来打开它
  2. 使用driver.findElements找到通过元素的下拉
  3. 迭代里面的选项全部webelements并拉出中的元素的文本。当你发现文本你期望进行点击

类似下面的可能是有用的:

public void clickDropdownOption(WebDriver driver, String option) throws Exception{ 
    waitForDropdownMenuToBeVisible(driver); 
    WebElement dropdownMenu = driver.findElement(dropdownMenuLocator); 
    List<WebElement> optionElements = dropdownMenu.findElements(dropdownOptionLocator); 
    for(int i=0; i < optionElements.size(); i++){ 
     if(optionElements.get(i).getText().equals(option)){ 
      click(driver, optionElements.get(i)); 
      waitForDropdownMenuToBeinvisible(driver); 
      return; 
     } 
    } 
    throw new Exception("Dropdown option " + option + " was not found"); 
} 

显然,如果这是一个正常的HTML下拉菜单,然后用传统的方法

+0

嗨Raymond ,我无法点击下拉菜单打开它,请参阅附件的DOM结构截图。 – Kushal

+0

@ user2447231你可以检查DOM中的其他地方是否有相同的ID? –

+0

不,Raymond,没有其他具有相同ID的元素,使用size()进行检查,并返回1。 – Kushal