1. Click to operate
Left click
driver.click()
Method for simulating a left mouse click
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys('python自动化测试')
driver.find_element(By.ID, 'su').click()
sleep(3)
driver.quit()
Right click
context_click(元素)
The method is used to simulate the right click operation of the mouse
- 【Method to realize】
-
ActionChains(driver).context_click(element).perform()
from selenium.webdriver import ActionChains
ActionChains(driver).context_click(element).perform()
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
element = driver.find_element(By.LINK_TEXT, '地图')
ActionChains(driver).context_click(element).perform()
sleep(3)
driver.quit()
2. Double-click operation
double_click(元素)
The method is used to simulate the double-click operation of the mouse
- 【Method to realize】
-
ActionChains(driver).double_click(element).perform()
from selenium.webdriver import ActionChains
ActionChains(driver).double_click(element).perform()
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
element = driver.find_element(By.LINK_TEXT, '地图')
ActionChains(driver).double_click(element).perform()
sleep(3)
driver.quit()
3. Drag and drop operation
drag_and_drop(起始元素位置,目标元素位置)
The method is used to simulate the drag operation of the mouse
- 【Method to realize】
-
ActionChains(driver).drag_and_drop(起始元素位置,目标元素位置).perform()
-
- The starting element position and the target element position can be obtained through the find_element() element positioning method
from selenium.webdriver import ActionChains
ActionChains(driver).drag_and_drop(起始元素位置,目标元素位置).perform()
4. Suspension operation
move_to_element(执行鼠标悬浮操作的元素)
- 【Method to realize】
-
ActionChains(driver).move_to_element(element).perform()
from selenium.webdriver import ActionChains
ActionChains(driver).move_to_element(element).perform()
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
element = driver.find_element(By.CSS_SELECTOR, '#s-usersetting-top')
ActionChains(driver).move_to_element(element).perform()
sleep(3)
driver.quit()