无法选择下拉菜单
问题描述:
我想自动使用使用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]
答
如果下拉菜单是从一种语言如角,然后使用选择可能不起作用。我的建议是我们硒的点击操作。
- 点击下拉菜单来打开它
- 使用driver.findElements找到通过元素的下拉
- 迭代里面的选项全部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下拉菜单,然后用传统的方法
分享请相关的HTML。 – DebanjanB
哪里截图..? –
请分享html DOM /网站网址。 –