Element ... is not clickable at point (x, y). Other element would receive the click" 可能由不同的因素引起。您可以通过以下任一程序解决它们:
- 由于存在 JavaScript 或 AJAX 调用,元素没有被点击
尝试使用Actions类:
WebElement element = driver.findElement(By.id("id1"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
- 元素没有被点击,因为它不在Viewport 内
尝试使用JavascriptExecutor 将元素带入视口:
JavascriptExecutor jse1 = (JavascriptExecutor)driver;
jse1.executeScript("scroll(250, 0)"); // if the element is on top.
jse1.executeScript("scroll(0, 250)"); // if the element is at bottom.
或者
WebElement myelement = driver.findElement(By.id("id1"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement);
- 页面在元素可点击之前被刷新。
在这种情况下诱导一些wait。
- 元素存在于 DOM 中但不可点击。
在这种情况下,添加一些 ExplicitWait 以使元素可点击。
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("id1")));
- 元素存在,但具有临时叠加层。
在这种情况下,ExplicitWait 将 ExpectedConditions 设置为 invisibilityOfElementLocated 以使叠加层不可见。
WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
- 元素存在,但具有永久叠加层。
使用JavascriptExecutor 直接在元素上发送点击。
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);