selenium - 在覆盖的情况下,Selenium webdriver wait 似乎不起作用
问题描述
当单击登录按钮时,会出现一个叠加层(灰色半透明屏幕)并停留几秒钟。因此,selenium web 驱动程序无法找到元素,因为这种覆盖类型将它们隐藏了一段时间,或者至少在我看来是这样。我该如何处理?我没找到Thread.sleep
这里不是一种有效的方式。
我试过了 -
public void login(){
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.invisibilityOfElementLocated((By.id("ajax-overlay"))));
wait.until(ExpectedConditions.elementToBeClickable((By.id("okbutton))));
driver.findElement(By.id("username)).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin123");
driver.findElement(By.id("okbutton")).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated((By.id("ajax-overlay"))));
}
但似乎没有任何效果,我仍然得到错误 -
org.openqa.selenium.WebDriverException: unknown error: Element <button id="loginDialog:okButton" name="loginDialog:okButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-panel-titlebar-icon synchronous" onclick="PrimeFaces.ab({source:'loginDialog:okButton',process:'loginDialog:okButton loginDialog:username loginDialog:password loginDialog:okButton',update:'startingCashFocus loginDialog:dialogFocus loginDialog:lblPassword loginDialog:lblUsername loginDialog:messages',oncomplete:function(xhr,status,args){handleLoginAttempt(xhr, status, args, loginWidget, null); ;}});return false;" tabindex="1" type="submit" role="button" aria-disabled="false">...</button> is not clickable at point (931, 250). Other element would receive the click: <div id="ajax-overlay" class="ui-blockui ui-widget-overlay ui-helper-hidden eternal" style="display: block;"></div>
此外,没有办法找出覆盖 id,谢天谢地,selenium 在其错误详细信息中给出了它。
解决方案
尝试使用以下方法之一单击元素,这将解决此异常:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id('okbutton'))).click().perform();
或者
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.id('okbutton')));
推荐阅读
- spring - 如何知道有多少个连接与 SpringBoot 网站连接?
- c++ - 使用 Xcode 和 Eclipse 在 Mac Mojave 上的着色器编译问题
- spring-data-redis - 如何在 Spring-Data-Redis 中为 RedisTemplate 使用连接事件?
- python - 如何在 Python 的 googleapis-common-protos 中使用 proto 文件?
- c++ - 使用指向 class::method 的指针数组调用方法
- azure - 让 Azure DevOps 审查 GitHub 拉取请求
- netlogo - 如何让特定补丁上的海龟做某事
- python - 如何更新模型并对这个对象或其他对象做一些事情?
- python - 为什么设置一个元素值会改变 Python 2d 矩阵中其他元素的值
- html - CSS 网格 + srcset + 尺寸适用于多种宽度和屏幕分辨率?