我使用了显式等待,并且收到警告:
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
从您的代码块中可以清楚地看出您已经定义了 wait
如WebDriverWait 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/