我正在尝试使用Java实现Selenium Webdriver.
基本上,我有一个空白字段的网站.用户单击该字段后,将出现一个包含5个选项的下拉列表,用户应选择一个选项.
代码看起来像这样
Department
Option 1
Option 2
Option 3
Option 4
Option 5
我尝试使用此Java代码来选择选项2
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".form-row.print-avoid-page-break>label")));
//Start to find the element. The ID is dynamically randomly generated by the system each time the page loads except the last part TaskID, thus looking for the string TaskID
Select dropdown = new Select (driver.findElement(By.xpath(".//*[contains(@id,'TaskId')]")));
dropdown.selectByValue("OPT2");
Selenium返回错误
org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
我觉得这是由< div class =“subhidden”>引起的,但我不太确定.
任何建议都非常感谢.谢谢.
解决方法:
下面测试过的代码块看起来很好.
看看html,这是一个公平的假设,即选择器不会唯一地返回intendet元素.我相信你想要包含id TaskId的Select标签.只需简单地做一个依赖于标签的搜索//可以选择[包含(@ id,’TaskId’)],因为< input id =“Schedule-00-Row136153aa-9fa8-499b-8458-2b155443223bE-TaskId-Display”也有id与相同的文本TaskIdand继续我建议你在插入测试之前测试xpath,至少从我的经验中说.
WebDriverWait wait = new WebDriverWait(driver, 100);
By selectElementSelector = By.xpath("//select[contains(@id,'TaskId')]");
WebElement selectElement = wait.until(ExpectedConditions.presenceOfElementLocated(selectElementSelector));
Select dropdown = new Select (selectElement);
dropdown.selectByValue("OPT2");
编辑
另一个可能的选择是使用适当的选择器查找元素以及选项.同样,您必须测试选择器以确保它是唯一的,这是您想要的.
WebDriverWait wait = new WebDriverWait(driver, 100);
String optionToSelect = "OPT1";
//Selector is the trick here
By selectElementSelector = By.cssSelector("select[id*='TaskId']>option[value='" + optionToSelect + "']");
wait.until(ExpectedConditions.presenceOfElementLocated(selectElementSelector)).click();
标签:java,selenium,select,xpath,webdriver
来源: https://codeday.me/bug/20190830/1765145.html