将鼠标悬停在使用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()并不意味着元素是可见的。相反,您需要为visibilityOfElementLocated()引入WebDriverWait,并且您可以使用以下定位器策略之一:

  • 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();
    

最新更新