我正在尝试使用 Selenium WebDriver 自动执行 Web 应用程序的登录过程。我找到了用户、密码 webelements,使用 sendKeys() 输入值,然后单击提交按钮,该按钮抛出异常,表示它不可见。网页 html 是这样的...
<button class="btn btn--loading login-form__btn login-form__btn--sms push-half--top clear--right float--right js-action js-action--submit js-action--submit-agreements" data-step="4" disabled="disabled" type="submit">
<span class="btn--loading__icon loader"></span>
<span class="btn--loading__label">Submit</span>
</button>
由 org.openqa.selenium.ElementNotVisibleException 引起:元素不可见
我使用了这样的代码...
WebElement userElement = wd.findElement(By.id(locators.get("xxxxxx.login.UNOusername.id")));
WebElement pwdElement = wd.findElement(By.id(locators.get("xxxxxx.login.UNOpassword.id")));
WebElement sbmElement = wd.findElement(By.xpath(locators.get("xxxxxx.login.UNOsubmit.button.xpath")))
userElement.sendKeys(defaultUser);
pwdElement.sendKeys(defaultPassword);
driver.sleep(2000);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(locators.get("firmsoft.login.UNOsubmit.button.xpath"))));
println ("Submit button is displayed ="+sbmElement.isDisplayed()); // returning 'false'
sbmElement.click(); // throwing exception.
手动执行此操作没有问题。一旦我在密码字段中输入几个字符,就会显示提交按钮并可单击。
我什至尝试通过此代码片段将鼠标悬停在上述按钮上。它弹出并看起来可见,但仍然提示同样的问题。
Actions build = new Actions(driver);
build.moveToElement(sbmElement).build().perform();
driver.sleep(5000);
缺少什么,我应该处理?
最佳答案
尝试使用 visibilityOf 预期条件而不是 presenceOfElementLocated 并查看它是否可以解决问题,即替换代码中的以下行:
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(locators.get("firmsoft.login.UNOsubmit.button.xpath"))));
具有以下内容:
wait.until(ExpectedConditions.visibilityOf(By.xpath(locators.get("firmsoft.login.UNOsubmit.button.xpath"))));
如果您仍然遇到同样的问题,请告诉我。
关于java - 如何处理登录页面中单击时 Selenium 报告为 "NOT visible"的 Web 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43059172/