Selenium WebDriver - 确定元素是否可点击(即不被dojo模态灯箱遮挡)

Mat*_*hew 17 ajax dojo selenium webdriver

我编写了自动脚本来测试非常重的ajax的Web应用程序.例如,Saving...在保存设置时会显示带有文本" " 的模态对话框,而灯箱会使页面的其余部分灰显.

我的测试脚本试图在消息消失之前单击测试中的下一个链接.它几乎总是在驱动Firefox时有效,但在驱动Chrome时会显示以下错误:

Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (99.5, 118.5). Other element would receive the click: <div class="dijitDialogUnderlay _underlay" dojoattachpoint="node" id="lfn10Dijit_freedom_widget_common_environment_Dialog_8_underlay" style="width: 1034px; height: 1025px; "></div> (WARNING: The server did not provide any stacktrace information)
Run Code Online (Sandbox Code Playgroud)

发生这种情况是因为灯箱遮挡了我想要点击的元素.我想在尝试点击链接之前等待灯箱消失.

检查灯箱不再存在不是一种有效的解决方法,因为有时会有多个级别的模态对话框和灯箱,并且没有简单的方法来区分它们.

在Selenium中是否有办法检测元素是否可点击(没有其他元素遮盖它)?尝试/捕获将是一种解决方法,但我更愿意做一个正确的检查,如果可能的话.

Pav*_*rin 20

使用WebDriverWait条件.

    WebDriverWait wait = new WebDriverWait(yourWebDriver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element")));
Run Code Online (Sandbox Code Playgroud)

Webdriver将等待5秒钟,以便能够单击您的元素.

  • 上次我尝试使用Chrome时它不起作用:尽管有其他图层阻止点击,但仍然满足"可点击"条件. (15认同)
  • 我不知道为什么这被OP接受了.`ExpectedConditions.ElementToBeClickable`检查元素是否为空,显示和启用 - 其中没有一个句柄被另一个元素覆盖. (6认同)
  • 不使用Firefox 20,selenium-firefox-driver 2.31.0.当元素被模式对话框阻止时,elementToBeClickable返回true (3认同)

小智 6

您可以使用ExpectedConditions.invisibilityOfElementLocated(By by)等待元素不可见或不存在于DOM上的方法.

WebDriverWait wait = new WebDriverWait(yourWebDriver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("yourSavingModalDialogDiv")));
Run Code Online (Sandbox Code Playgroud)

因此,根据模态对话框不可见或取消DOM的时间,webdriver将等待.等待最多10秒.