Selenium Web 驱动程序和 Java。元素在点 (x, y) 处不可点击。其他元素会收到点击

新手上路,请多包涵

我使用了显式等待,但我收到了警告:

org.openqa.selenium.WebDriverException:元素在点 (36, 72) 处不可点击。其他元素会收到点击:… 命令持续时间或超时:393 毫秒

如果我使用 Thread.sleep(2000) 我不会收到任何警告。

 @Test(dataProvider = "menuData")
public void Main(String btnMenu, String TitleResultPage, String Text) throws InterruptedException {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    driver.findElement(By.id("navigationPageButton")).click();

    try {
       wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
    } catch (Exception e) {
        System.out.println("Oh");
    }
    driver.findElement(By.cssSelector(btnMenu)).click();
    Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(), Text);
}

原文由 Maria 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 678
1 个回答

WebDriverException: 元素在点 (x, y) 不可点击

这是一个典型的 org.openqa.selenium.WebDriverException ,它扩展了 java.lang.RuntimeException

此异常的字段是:

  • BASE_SUPPORT_URL : protected static final java.lang.String BASE_SUPPORT_URL
  • 驱动程序信息:--- public static final java.lang.String DRIVER_INFO
  • 会话 ID:— public static final java.lang.String SESSION_ID

关于您的个人用例,错误说明了一切:

 WebDriverException: Element is not clickable at point (x, y). Other element would receive the click

It is clear from your code block that you have defined the wait as WebDriverWait wait = new WebDriverWait(driver, 10); but you are calling the click() method on the element before the ExplicitWaituntil(ExpectedConditions.elementToBeClickable) 中发挥作用。

解决方案

错误 Element is not clickable at point (x, y) 可能由不同的因素引起。您可以通过以下任一过程解决它们:

1. 由于存在 JavaScript 或 AJAX 调用,元素未被点击

尝试使用 Actions 类:

 WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

2. 元素未被点击,因为它不在 视口

尝试使用 JavascriptExecutor 将元素放入视口中:

 WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement);

3. 页面在元素可点击之前刷新。

在这种情况下,如第 4 点所述,引入 ExplicitWait ,即 WebDriverWait

4. 元素存在于 DOM 中但不可点击。

在这种情况下,将 --- ExpectedConditions 设置为 elementToBeClickable 以使元素可点击:

 WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));

5. 元素存在但有临时覆盖。

在这种情况下, ExplicitWaitExpectedConditions 设置为 invisibilityOfElementLocated 以使覆盖层不可见。

 WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));

6. 元素存在但具有永久覆盖。

使用 JavascriptExecutor 直接在元素上发送点击。

 WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题