如何在Python的Selenium中使用Action Chain类的click()方法?
我们可以使用Selenium中Action Chain类的click()方法。这些类通常用于自动化交互,例如上下文菜单点击、鼠标按钮操作、按键和鼠标移动。
这些类型的操作主要在复杂的场景中很常见,例如拖放和将鼠标悬停在页面上的元素上。Action Chains类的使用方法由高级脚本利用。我们可以借助Selenium中的Action Chains来操作DOM。
Action chain对象以队列的形式实现ActionChains,然后执行perform()方法。调用perform()方法后,将执行action chains上的所有操作。
创建Action Chain对象的方法如下:
首先,我们需要导入Action Chain类,然后将驱动程序作为参数传递给它。
现在,可以使用此对象执行所有action chains的操作。
语法
创建Action Chains对象的语法:
from selenium import webdriver
# import Action chains
from selenium.webdriver import ActionChains
# create webdriver object driver = webdriver.Firefox() # create action chain object action = ActionChains(driver)
创建Action Chains对象后,我们可以像队列一样一个接一个地执行许多操作。
click() - 此方法对页面上的元素执行点击操作。
语法
click(args)
其中args是要点击的元素。如果省略参数,它将点击鼠标当前位置。
#element
source = driver.find_element_by_id("name")
#action chain object
action = ActionChains(driver)
# click operation
action.click(source)
# perform the action
action.perform()示例
点击元素的代码实现。
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then
#invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.com/about/about_careers.htm")
#to refresh the browser
driver.refresh()
# identifying the source element
source= driver.find_element_by_xpath("//*[text()='Team']");
# action chain object creation
action = ActionChains(driver)
# click the element
action.click(source)
# perform the action
action.perform()
#to close the browser
driver.close()
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP