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.拖動,方法類似,在此不列舉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM