• 引入依赖**from **selenium.webdriver.common.action_chains **import **ActionChains
  • ActionChains放入driver,action = ActionChains(driver)
  • S时间操作必须执行action.perform()

    01.右键

    context_click
    执行代码 ```html from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains import time

webdriver 获取浏览器的对象

from selenium.webdriver.support.select import Select

d = Service(“chromedriver.exe”) driver = webdriver.Chrome(service=d)

s = Service(“chromedriver.exe”)

driver = webdriver.Chrome(service=s)

获取网址

url=”https://www.baidu.com/“ driver.get(url)

窗口最大化

driver.maximize_window() action = ActionChains(driver)

点击右键

action.context_click(driver.find_element(By.ID,”kw”))

操作必须要执行

action.perform()

time.sleep(4)

隐式等待

driver.implicitly_wait(4)

回收数据

driver.quit()

  1. <a name="e8uyE"></a>
  2. ## 02.鼠标悬停
  3. `move_to_element`<br />执行代码
  4. ```html
  5. from selenium import webdriver
  6. from selenium.webdriver.chrome.service import Service
  7. from selenium.webdriver.common.by import By
  8. from selenium.webdriver.common.action_chains import ActionChains
  9. import time
  10. #webdriver 获取浏览器的对象
  11. from selenium.webdriver.support.select import Select
  12. d = Service("chromedriver.exe")
  13. driver = webdriver.Chrome(service=d)
  14. # s = Service("chromedriver.exe")
  15. # driver = webdriver.Chrome(service=s)
  16. #获取网址
  17. url="https://www.baidu.com/"
  18. driver.get(url)
  19. # 窗口最大化
  20. driver.maximize_window()
  21. action = ActionChains(driver)
  22. # 点击右键
  23. # action.context_click(driver.find_element(By.ID,"kw"))
  24. # 点击鼠标悬停
  25. action.move_to_element(driver.find_element(By.CLASS_NAME,"soutu-btn"))
  26. # 时间操作必须要执行
  27. action.perform()
  28. time.sleep(4)
  29. # 隐式等待
  30. # driver.implicitly_wait(4)
  31. #回收数据
  32. driver.quit()

03.鼠标拖拽

将ID为div1拖拽到ID为div2的盒子
action.drag_and_drop(driver.find_element(By.ID,**"div1"**) , driver.find_element(By.ID,**"div2"**))

04.鼠标双击

action.double_click(driver.find_element(By.ID,**"kw"**))