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

Mar*_*ria 63 java selenium webdriver selenium-webdriver

我使用了明确的等待,我有警告:

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);
}
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 147

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 
Run Code Online (Sandbox Code Playgroud)

从你的代码块中可以清楚地看出,你已经定义了waitas,WebDriverWait wait = new WebDriverWait(driver, 10);但是click()ExplicitWait进入游戏之前你正在调用元素上的方法until(ExpectedConditions.elementToBeClickable).

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

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

尝试使用ActionsClass:

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

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

尝试使用JavascriptExecutor该元素在视口中:

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

3.在元素可点击之前,页面将被刷新.

在这种情况下,如第4点所述诱导ExplicitWaitWebDriverWait.

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

在这种情况下, 请将ExplicitWaitExpectedConditions设置elementToBeClickable为可单击的元素:

WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));
Run Code Online (Sandbox Code Playgroud)

5.元素存在但具有临时叠加.

在这种情况下,ExplicitWait使用 ExpectedConditionsset设置invisibilityOfElementLocated为Overlay是不可见的.

WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
Run Code Online (Sandbox Code Playgroud)

6.元素存在但具有永久叠加.

用于JavascriptExecutor直接在元素上发送单击.

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

  • 您已经介绍了许多可能性,其中只有5和6是处理上述错误的正确方法.前四个抛出不同的错误,你给出的解决方案将无法正常工作.对于一个示例,第3点实际上是一个陈旧的元素问题,即使您使用elementToBeClickble方法等待多长时间也无法工作.这必须以不同的方式处理. (3认同)
  • 需要注意的重要一点是,当我们模拟用户的操作时,使用 javascript 单击根本无法单击的元素可能是非常不可取的(#6)。最终用户永远不会这样做,他们只会滚动到元素以将其带入视口或消除任何覆盖层(如果页面允许)与其交互。 (2认同)

小智 12

如果您需要使用Javascript

我们可以使用参数[0] .click()来模拟点击操作.

var element = element(by.linkText('webdriverjs'));
browser.executeScript("arguments[0].click()",element);
Run Code Online (Sandbox Code Playgroud)


fg7*_*8nc 2

你可以试试

WebElement navigationPageButton = (new WebDriverWait(driver, 10))
 .until(ExpectedConditions.presenceOfElementLocated(By.id("navigationPageButton")));
navigationPageButton.click();
Run Code Online (Sandbox Code Playgroud)