ActionChains类鼠标操作的常用方法:
引入ActionChains类:from selenium.webdriver.common.action_chains import ActionChains
1.右击
方法:context_click()
实例:ActionChains(driver).context_click(driver.find_element_by_id("id")).perform()
--------------------------------------------------------------------------------
2.双击
方法:double_click()
实例:ActionChains(driver).double_click(driver.find_element_by_name("name")).perform()
--------------------------------------------------------------------------------
3:鼠标拖放
方法:drag_and_drop(source, target)
source:鼠标按下的源元素;target:鼠标释放的目标元素
实例:element = driver.find_element_by_name("name")
target = driver.find_element_by_name("name")
ActionChains(driver).drag_and_drop(element, target).perform()
--------------------------------------------------------------------------------
4:鼠标悬停在一个元素上(hover)
方法:move_to_element()
实例:above = driver.find_element_by_xpath("xpath路径")
ActionChains(driver).move_to_element(above).perform()
--------------------------------------------------------------------------------
5:按下鼠标左键在一个元素上
方法:click_and_hold()
实例:left = driver.find_element_by_name("name")
ActionChains(driver).click_and_hold(left).perform()
--------------------------------------------------------------------------------
键盘事件:
引入Keys类包:from selenium.webdriver.common.keys import Keys
--------------------------------------------------------------------------------
6:输入
方法:send_keys()
实例:driver.find_element_by_id("id").send_keys("XXX")
--------------------------------------------------------------------------------
7:输入空格
方法:send_keys(Keys.SPACE)
实例:driver.find_element_by_id("id").send_keys(Keys.SPACE)
--------------------------------------------------------------------------------
8:ctrl + a
全选输入框的内容
ctrl + x
剪切输入框的内容
ctrl + v
粘贴到输入框
ctrl + c
复制
方法:send_keys(Keys.CONTROL,‘a‘)
实例:driver.find_element_by_id("id").send_keys(Keys.CONTROL,‘a‘)
--------------------------------------------------------------------------------
9:回车代替点击
方法:send_keys(Keys.ENTER)
实例:driver.find_element_by_id("id").send_keys(Keys.ENTER)
--------------------------------------------------------------------------------
10:制表键(Tab)
方法:send_keys(Keys.TAB)
实例:driver.find_element_by_id("id").send_keys(Keys.TAB)
--------------------------------------------------------------------------------
11:回退键(Esc)
方法:send_keys(Keys.ESCAPE)
实例:driver.find_element_by_id("id").send_keys(Keys.ESCAPE)
原文地址:https://www.cnblogs.com/wyj497022944/p/9626808.html