藉助 Selenium 創造類人化滑鼠移動。


我們可以藉助 Selenium 實現類人化的滑鼠移動。可以藉助 Actions 類進行此操作。類人化的滑鼠移動包括右鍵單擊、雙擊、滑鼠移動、拖放操作等。

要進行滑鼠移動,請使用 moveToElement 方法。要執行右鍵單擊操作,請使用 contextClick 方法。最後,要真正執行操作,應新增 build perform 方法。

一旦我們右鍵單擊一個元素,就會顯示各種選項。我們必須新增** import org.openqa.selenium.interactions.Actions 包來實現操作。

示例

程式碼實現。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class RightClick{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      String u = " https://tutorialspoint.tw/about/about_careers.htm";
      driver.get(u);
      // identify element
      WebElement m=driver.findElement(By.xpath("//*[text()='Careers']"));
      // moveToElement and contextClick
      Actions act = new Actions(driver);
      act.moveToElement(m).contextClick().build().perform();
   }
}

輸出

更新日期:2020 年 12 月 28 日

3K+ 瀏覽人數

開啟您的職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.