Selenium 简明教程
Selenium Webdriver - Right Click
Selenium 可以借助 ActionsChains 类执行鼠标移动、按键、悬停在元素上、右键单击、拖放操作等动作。method context_click 对元素执行右击或上下文单击操作。
Selenium can perform mouse movements, key press, hovering on an element, right-click, drag and drop actions, and so on with the help of the ActionsChains class. The method context_click performs right-click or context click on an element.
用于右击或上下文单击的 syntax 如下所示 −
The syntax for using the right click or context click is as follows −
context_click(e=None)
此处,e 是要右击的元素。如果提到了“None”,则单击在当前鼠标位置执行。我们必须添加来自导入 selenium.webdriver 的语句 ActionChains 才能使用 ActionChains 类。
Here, e is the element to be right-clicked. If ‘None’ is mentioned, the click is performed on the present mouse position. We have to add the statement from selenium.webdriver import ActionChains to work with the ActionChains class.
Code Implementation
用于右击或上下文单击的代码实现如下 −
The code implementation for using the right click or context click is as follows −
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://www.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()