如何透過 Selenium 中的操作在元素上執行右鍵單擊?


藉助操作,我們可以對 Selenium 中的元素執行右鍵單擊。為了執行右鍵單擊操作,我們將使用 contextClick () 方法。首先,我們需藉助 moveToElement () 方法將滑鼠移動到特定元素,然後使用 contextClick () 方法進行右鍵單擊。最後,使用 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 RightClick{
   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.tw/questions/index.php";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // contextClick() method for right click to an element after moving the //mouse to with the          moveToElement()
      Actions a = new Actions(driver);
      a.moveToElement(driver.findElement(By.xpath(“input[@type=’text’]))).
      contextClick().
      build().perform();
      driver.quit();
   }
}

更新時間: 2020 年 6 月 10 日

3K+ 次瀏覽

啟動你的 職業生涯

完成課程並獲得認證

開始
廣告