java - Selenium Webdriver 和 Java。元素在点 (x, y) 处不可单击。其他元素将收到点击

标签 java selenium selenium-webdriver webdriver

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

org.openqa.selenium.WebDriverException: Element is not clickable at point (36, 72). Other element would receive the click: ... Command duration or timeout: 393 milliseconds

如果我使用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);
}

最佳答案

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

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

此异常的字段是:

  • BASE_SUPPORT_URL :protected static final java.lang.String BASE_SUPPORT_URL
  • DRIVER_INFO :public static final java.lang.String DRIVER_INFO
  • SESSION_ID :public static final java.lang.String SESSION_ID
<小时/>

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

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

从您的代码块中可以清楚地看出您已经定义了 waitWebDriverWait wait = new WebDriverWait(driver, 10);但您正在调用 click() ExplicitWait 之前的元素上的方法发挥作用如 until(ExpectedConditions.elementToBeClickable)

解决方案

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

<强>1。由于存在 JavaScript 或 AJAX 调用,元素未被点击

尝试使用Actions类(class):

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

<强>2。元素未被单击,因为它不在 Viewport

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

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

<强>3。在元素可点击之前页面已刷新。

在这种情况下,会引发ExplicitWait,即第 4 点中提到的 WebDriverWait

<强>4。元素存在于 DOM 中但不可点击。

在这种情况下,使用 ExpectedConditions 诱导 ExplicitWait设置为elementToBeClickable使元素可点击:

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

<强>5。元素存在,但具有临时覆盖。

在这种情况下,诱导 ExplicitWait ExpectedConditions 设置为 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);

关于java - Selenium Webdriver 和 Java。元素在点 (x, y) 处不可单击。其他元素将收到点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59716602/

相关文章:

javascript - 检查 Selenium 中多个页面的标题

java - 无法将 URL 发送到 Chrome

python - 断管错误 selenium webdriver,当命令之间存在间隙时?

java - 创建客户端-服务器桌面应用程序

java - 为什么我的应用程序中没有启用关闭端点?

java - Switch 语句不允许我将 ArrayList 的最后一个元素添加到新的 ArrayList 中

java - Derby java -derbyrun.jar server 将嵌入模式更改为服务器模式

javascript - 遍历父元素并选择子元素

python - 如何向自动生成的 Python Selenium 脚本添加参数

c# - Selenium Webdriver - 如何为 Firefox 设置代理到 "auto-detect"