单击 Selenium 中被覆盖层隐藏的元素
在基于 Selenium 的自动化中,单击被覆盖层遮挡的元素可能是一个常见的挑战。错误消息“Element MyElement is not clickable at point (x, y)... Other element will receive the click”表示这种情况。
解决问题
要解决此问题,请考虑以下方法:
WebElement element = driver.findElement(By.id("id1")); Actions actions = new Actions(driver); actions.moveToElement(element).click().build().perform();
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.
页面刷新:
如果页面在元素变得可点击之前刷新,则引发等待:
Thread.sleep(500); // replace 500 with an appropriate timeout in milliseconds
WebDriverWait wait2 = new WebDriverWait(driver, 10); wait2.until(ExpectedConditions.elementToBeClickable(By.id("id1")));
WebDriverWait wait3 = new WebDriverWait(driver, 10); wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
WebElement ele = driver.findElement(By.xpath("element_xpath")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", ele);
以上是如何点击 Selenium 中覆盖层隐藏的元素?的详细内容。更多信息请关注PHP中文网其他相关文章!