selenium之鼠标的操作(python)
鼠标操作的方法,封装在ActionChains类中
perform:执行ActionChains中的所有存储行为
context_click:右键单击
move_to_element:悬停
double_click:双击
drag_and_drop:拖动
1.右击操作
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 from selenium import webdriver 4 from selenium.webdriver import ActionChains 5 from time import sleep 6 7 driver = webdriver.Firefox() 8 driver.get("https://www.baidu.com/") 9 # 右击操作 10 sleep(5) 11 right_click = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[3]/a[1]") #新闻 12 ActionChains(driver).context_click(right_click).perform()
2.悬停
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 from selenium import webdriver 4 from selenium.webdriver import ActionChains 5 from time import sleep 6 7 driver = webdriver.Firefox() 8 driver.get("https://www.baidu.com/") 9 # 悬停操作 10 sleep(5) 11 on_stop = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[3]/a[8]") #设置 12 ActionChains(driver).move_to_element(on_stop).perform()
3.双击
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 from selenium import webdriver 4 from selenium.webdriver import ActionChains 5 from time import sleep 6 7 driver = webdriver.Firefox() 8 driver.get("https://www.baidu.com/") 9 # 双击操作 10 sleep(5) 11 double_click = driver.find_element_by_xpath('//*[@id="su"]') #百度一下 12 ActionChains(driver).double_click(double_click).perform()
4.拖动,方法类似,在此不列举