如何使用 selenium 代码为 firefox 选择 Ajax 下拉建议列表项?
我的问题是:Ajax 下拉列表是可见的,但未被选中,后续步骤会卡住。 可能是 Selenium 在等待什么。
页面填充的列表是动态的并且在 bla bla 标签中。 请帮助示例代码。 我如何在这里使用 waitfor* 。 请记住,我没有使用 firefox ide,但我正在编写代码。 请帮忙。
最佳答案
我有一个类似的问题,selenium 能够找到下拉菜单但无法点击可见文本。后来我发现有一个 Ajax 调用正在填充下拉菜单数据,因此 selenium 似乎无法选择预期的可见文本,因为列表项尚未完全填充。也就是说,当脚本选择我的选项值时,Ajax 尚未完全加载菜单选项。这是我的解决方案:
public void nameOfCollegeList(String optionItem) {
// declare the dropdownMenu web element
WebElement dropDownMenu = driver.findElement(By.cssSelector("#CollegeNames"));
// click on the dropdownMenu element to initiate Ajax call
dropDownMenu.click();
// keep checking the drop down menu item list until you find the desired text that indicates that the menu has
// been fully loaded. In this example I always expect "Other (please specify)" to be the last item in the drop down menu.
// If I don't find the expected last item in the list in my if condition, execute the else condition by calling the
// same method(recursively). Please note that if the "if" statement is never satisfied then you'll end up with an
// infinite loop.
if (dropDownMenu.getText().contains("Other (please specify)")) {
new Select(dropDownMenu).selectByVisibleText(optionItem);
}
else {
nameOfCollegeList(optionItem);
}
}
关于ajax - 使用 Selenium for Firefox 选择 Ajax 下拉建议列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6565220/