Web automation: 5.1 selenium mouse operation - click, double click, right click, hover, drag and drop

Fixed usage:

# 鼠标操作的固定用法:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()  # 初始化一个谷歌浏览器对象
el = driver.find_element('xpath', 'vaule')   # xpath元素定位
action = ActionChains(driver)   # 初始化一个动作对象
action.double_click(el).perform()   # 执行对应的动作:双击

Click on:

action.click(el).perform()

Double click:

action.double_click(el).perform() 

Right click:

action.context_click(el).perform()

hover:

action.move_to_element(el).perform()

Drag and drop:

# 从一个元素start_el到另一个元素end_el
action.drag_and_drop(start_el, end_el).perform()

practise:

Baidu –> Settings (hover) –> Advanced Settings
insert image description here

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com/")   # 打开网页:百度

# 第一步:鼠标移动到“设置”按钮
el = driver.find_element('xpath', '//span[@id="s-usersetting-top"]')   # 定位元素:按钮“设置”
action = ActionChains(driver)
action.move_to_element(el).perform()
# 第二:再点击“高级搜索”
el = driver.find_element('xpath', '//a[text()="高级搜索"]')   # 定位元素:按钮“高级设置”
el.click()

time.sleep(3)   # 等待3秒
driver.close()  # 关闭浏览器

Guess you like

Origin blog.csdn.net/weixin_48415452/article/details/120134808