Selenium Web驱动程序和Java元素在(x,y)点处不可单击其他元素将获得点击?

问题描述

你可以通过以下任一过程解决它们:

1.由于存在JavaScript或AJAX调用而无法单击元素

尝试使用ActionsClass

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.在元素可单击之前,页面正在刷新。

在这种情况下,请诱导ExplicitWait,即第4点中提到的WebDriverWait

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

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

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

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

在这种情况下,ExplicitWait使用 ExpectedConditions设置invisibilityOfElementLocated为可使Overlay不可见。

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

解决方法

Selenium 单击

我使用了明确的等待,并发出警告:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...