【问题标题】:Selenium webdriver wait doesn't seem to work in case of overlay在覆盖的情况下,Selenium webdriver wait 似乎不起作用
【发布时间】:2018-06-22 13:33:00
【问题描述】:

当点击登录按钮时,会出现一个叠加层(灰色半透明屏幕)并停留几秒钟。正因为如此,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 在其错误详细信息中给出了它。

【问题讨论】:

  • 请分享您点击元素的试用代码,在此行之后:wait.until(ExpectedConditions.elementToBeClickable((By.id("okbutton"))));
  • 更新了问题。请看一看。
  • 请更新您的问题标题,这与 webdriver 等无关。您的异常详细信息说明了其他内容。

标签: selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

尝试使用以下方法之一单击元素,这将解决此异常:

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')));

【讨论】:

  • 谢谢你,石田。第二个有效。第一个没有,原因很明显,我认为好像它会起作用,然后正常点击也可以起作用,因为点击已经完成但在叠加层上。感谢您的帮助。
  • @apun 请点赞,如果您认为它帮助您解决了问题。
  • 当然可以。我已经选择它作为答案。投票赞成。谢谢,肯定有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多