• Selenium Video Tutorials

Selenium WebDriver - 右键点击



Selenium 可以借助 ActionsChains 类执行鼠标移动、按键、悬停在元素上、右键点击、拖放等操作。context_click 方法对元素执行右键点击或上下文点击。

使用右键点击或上下文点击的语法如下:

context_click(e=None)

这里,e 是要右键点击的元素。如果提到“None”,则在当前鼠标位置执行点击。我们必须添加语句 from selenium.webdriver import ActionChains 来使用 ActionChains 类。

代码实现

使用右键点击或上下文点击的代码实现如下:

from selenium import webdriver from selenium.webdriver import ActionChains driver = webdriver.Chrome(executable_path='../drivers/chromedriver') #implicit wait time driver.implicitly_wait(5) #url launch driver.get("https://tutorialspoint.com/about/about_careers.htm") #identify element s = driver.find_element_by_xpath("//*[text()='Company']") #object of ActionChains a = ActionChains(driver) #right click then perform a.context_click(s).perform()

输出

Right Click

执行后,名为 - Company 的链接已被右键点击,并且所有新的选项都作为右键点击的结果显示。

广告