如何在 Selenium 中对元素执行鼠标悬停操作?


我们可以使用 Actions 类的帮助在 Selenium 中对元素执行鼠标悬停操作。为了执行鼠标移动,我们将使用 Actions 类的 moveToElement() 方法。最后,使用 build().perform() 执行所有步骤。

示例

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class MouseOver{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://tutorialspoint.com/questions/index.php";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // actions class with moveToElement() method for mouse hover to element
      Actions a = new Actions(driver);
      a.moveToElement(driver.findElement(By.xpath(“input[@type=’text’]))).
      build().perform();
      driver.quit();
   }
}

更新时间:2020 年 6 月 10 日

4K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.