仅将鼠标悬停在使用硒的元素上

问题描述 投票:0回答:2

我正在尝试使用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查找元素,也会遇到相同的错误。我在其他线程上看到该元素必须是特定的。

我在这里想念什么?

java selenium xpath css-selectors webdriverwait
2个回答
0
投票

presenceOfElementLocated()不保证该元素是visible。相反,您需要为visibilityOfElementLocated()引入WebDriverWait,并且可以使用以下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();
    

0
投票

此Web元素是否存在其他定位符,例如类名,类型,有时Web元素将具有内部子元素,如果您在以下子元素或同级中观察到isClickable属性,请使用它代替该Web元素。您可以为该Web元素发布HTML代码吗?很容易回答这个问题

© www.soinside.com 2019 - 2024. All rights reserved.