[Python automated testing]: Simulate mouse operation

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()
# 导入ActionChains类
# perform()方法:ActionChains类中封装的一个方法,若要执行ActionChains类中提供的鼠标事件方法,需要调用perform()方法才能真正执行
from selenium.webdriver import ActionChains
# 鼠标右击操作实际操作语法
ActionChains(driver).context_click(element).perform()
  • 【Example】
# 导包
from selenium import webdriver
# 导入ActionChains类
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")
# 定位到“地图”链接,结果返回定位到的元素,赋值给elemenet
element = driver.find_element(By.LINK_TEXT, '地图')
# 在“地图”链接上执行右击操作
# 调用ActionChains类中的鼠标事件右击操作,最后调用perform方法才能真正执行
ActionChains(driver).context_click(element).perform()

# 页面停留3秒后关闭
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()
# 导入ActionChains类
# perform()方法:ActionChains类中封装的一个方法,若要执行ActionChains类中提供的鼠标事件方法,需要调用perform()方法才能真正执行
from selenium.webdriver import ActionChains
# 鼠标s双击操作实际操作语法
ActionChains(driver).double_click(element).perform()
  • 【Example】
# 导包
from selenium import webdriver
# 导入ActionChains类
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")
# 定位到“地图”链接,结果返回定位到的元素,赋值给elemenet
element = driver.find_element(By.LINK_TEXT, '地图')

# 调用ActionChains类中的鼠标事件方法,最后调用perform方法才能真正执行
# 在“地图”链接上执行双击操作
ActionChains(driver).double_click(element).perform()

# 页面停留3秒后关闭
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
# 导入ActionChains类
# perform()方法:ActionChains类中封装的一个方法,若要执行ActionChains类中提供的鼠标事件方法,需要调用perform()方法才能真正执行
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()
# 导入ActionChains类
from selenium.webdriver import ActionChains
# 对定位到的“设置”元素执行悬停操作
ActionChains(driver).move_to_element(element).perform()
  • 【Example】
# 导包
from selenium import webdriver
# 导入ActionChains类
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()

# 页面停留3秒后关闭
sleep(3)
driver.quit()

Guess you like

Origin blog.csdn.net/Lucifer__hell/article/details/129545701