当元素存在于 DOM 中并且在视口(viewport)中可见时,以下代码可以完美运行:
WebElement button = webdriver.findElement(By.id("myButton"));
但是,我不明白为什么当按钮存在于 DOM 中但在视口(viewport)中不可见时它会抛出异常。
我确信该按钮存在:这里没有使用 ajax。
如何测试某个元素是否存在于 DOM 中,无论该元素在视口(viewport)中是否可见?
我已经在 stackoverflow 上看到了一些有用的链接:
https://stackoverflow.com/a/44916498/420593
How to check if an element is into view using Selenium WebDriver?
最佳答案
您可以将 WebDriverWait 与操作类一起使用来移动到元素并对其执行单击。
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(By.id("myButton")));
Actions actions = new Actions(driver);
actions.moveToElement(button).click().build().perform();
关于java - 当元素存在于页面但在视口(viewport)中不可见时,Selenium findElement 会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60887855/