如何使用 Selenium ChromeDriver 执行右键单击?
我们可以使用 Selenium ChromeDriver 来执行右键单击。在 Web 元素上右键单击时,上下文菜单将显示。例如,如果我们在文本区域上单击鼠标右键,就会弹出一个包含几个选项的附加菜单。
Selenium 中的 Actions 类负责模拟此鼠标操作。Actions 类的提供的contextClick() 方法来执行此操作,从上下文菜单中选择任何选项。
要执行此整个操作,我们首先要使用**moveToElement() 方法将鼠标移动到元素的中间,然后应用 contextClick() 方法。此外,我们必须执行 build() 方法来执行这两个操作,然后执行 perform() 方法实际执行操作。

我们必须将导入 org.openqa.selenium.interactions.Actions 包到我们的代码中,以使用 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 RightClickOperation{
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/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify element
WebElement l=driver.findElement(By.xpath("//*[text()='Library']"));
// Actions class with moveToElement() and contextClick()
Actions a = new Actions(driver);
a.moveToElement(l).contextClick().build().perform();
driver.quit();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP