Selenium 简明教程
Selenium Webdriver - Double Click
Selenium 可以使用 ActionsChains 类来执行鼠标移动、按键、悬停在元素上、双击、拖放操作等。method double_click 对某个元素执行双击操作。
Selenium can perform mouse movements, key press, hovering on an element, double click, drag and drop actions, and so on with the help of the ActionsChains class. The method double_click performs double-click on an element.
使用双击的 syntax 如下:
The syntax for using the double click is as follows:
double_click(e=None)
此处,e 是要双击的元素。如果指定了 None,则单击将按当前鼠标位置执行。我们必须添加语句 from selenium.webdriver import ActionChains 才能通过 ActionChains 类进行操作。
Here, e is the element to be double-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.
让我们在下面的元素上执行双击操作:
Let us perform the double click on the below element −
在上述图片中,可以看到在双击 Double Click me! 按钮时,会生成一个警报框。
In the above image, it is seen that on double clicking the Double Click me! button, an alert box gets generated.
Code Implementation
使用双击的代码实现如下:
The code implementation for using the double click is as follows −
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.alert import Alert
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("http://www.uitestpractice.com/Students/Actions")
#identify element
s = driver.find_element_by_name("dblClick")
#object of ActionChains
a = ActionChains(driver)
#right click then perform
a.double_click(s).perform()
#switch to alert
alrt = Alert(driver)
# get alert text
print(alrt.text)
#accept alert
alrt.accept()
#driver quit
driver.quit()
Output
输出显示消息 - 进程退出代码 0,表示上述 Python 代码成功执行。此外,警告文本 - 双击!会打印在控制台中。通过双击 Double Click me! 按钮生成了警告。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the Alert text - Double Clicked! gets printed in the console. The Alert got generated by double clicking the Double Click me! button.