java - 仅使用 Selenium 将鼠标悬停在元素上
问题描述
我试图简单地将鼠标悬停在使用 Selenium 代码的元素上。该元素出现在网页中并且是可点击的。
我不断收到此错误:
javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite. (Session info: chrome=80.0.3987.132) Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: 'TALANGEL-LP', ip: '172.17.17.148', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_152-release' Driver info: org.openqa.selenium.chrome.ChromeDriver
我的代码:
WebDriverWait wait = new WebDriverWait(browser,3);
elementToHoverOn = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("button[id='MyBtn']")));
new Actions(browser).moveToElement(elementToHoverOn).perform();
即使我尝试简单地使用 By.ID 查找元素,我也会得到相同的错误。我在其他线程上看到该元素必须是特定的,并且确实如此。
我在这里想念什么?
解决方案
presenceOfElementLocated()
不保证元素是可见的。相反,您需要诱导WebDriverWait并且visibilityOfElementLocated()
您可以使用以下任一 Locator Strategies:
cssSelector
:new Actions(browser).moveToElement(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button#MyBtn")))).build().perform();
xpath
:new Actions(browser).moveToElement(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@id='MyBtn']")))).build().perform();
推荐阅读
- excel - 将一列值排序到格式化的工作表中以打印标签
- ionic3 - 打开带有顶部滚动条的侧边菜单
- html - 为什么 ::after 伪元素出现在复选框输入而不是 Chrome 中的文本输入?
- selenium-webdriver - 使用 Selenium 网络驱动程序的鼠标悬停
- if-statement - 如何使用 yacc 正确实现 if 语句?
- node.js - npm install 不安装依赖项
- c++ - 链表(来自 'Node 的无效转换
*' 到 'int' [-fpermissive]|) - unit-testing - 在 BDD 中测试应用程序状态
- apache-kafka - Spring Kafka Consumer,倒回消费者偏移量以返回“n”条记录
- docker - 了解 Kubernetes 中的服务?