selenium之鼠标动作链

定位除了使用xpath,css选择器定位外,还可以使用鼠标动作链来进行定位;

  • 导入模块:from selenium.webdriver import ActionChains
  • 鼠标移动事件:
    • ActionChains(driver).move_to_element(元素的位置).perform()
  • 鼠标左击事件:
    • ActionChains(driver).click(元素的位置).perform()
  • 鼠标左双击事件:
    • ActionChains(driver).double_click(元素的位置).perform()
  • 鼠标右击事件:
    • ActionChains(driver).context_click(元素的位置).perform()
  • 鼠标左击并保持:
    • ActionChains(driver).click_and_hold(元素的位置).perform()
  • 将元素拖到指定元素并松开:
    • ActionChains(driver).drag_and_drop(原元素的位置, 新元素的位置).perform()
  • 将元素拖到指定坐标位置(以当前元素为起点):
    • ActionChains(driver).move_by_offset(x坐标, y坐标).perform()
  • 释放鼠标点击状态:
    • ActionChains(driver).release(xxx).perform()

基础案例:

python 复制代码
import time
from selenium.webdriver import Edge
from selenium.webdriver.common.keys import Keys
# 创建edge浏览器对象
driver = Edge()
# 打开浏览器访问网址
driver.get("https://www.lagou.com/")
# 提取`全国`标签的位置
el = driver.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a')  # selenium4.0后废除该方法
# 鼠标点击时间 -- 点击el对象
el.click()
time.sleep(1)
# 找到文本框,输入指定的内容 -- 然后按回车
driver.find_element_by_xpath('//*[@id="search_input"]').send_keys("python", Keys.ENTER)
# 找到元素的所在标签位置 -- 存在li_list列表内
li_list = driver.find_elements_by_xpath('//*[@id="s_position_list"]/ul/li')
# 遍历li_list列表,并取出相应的内容
for li in li_list:
    job_firm = li.find_elements_by_xpath('./div/div[2]/div/a')[0].text
    job_name = li.find_elements_by_tag_name('h3')[0].text
    job_money = li.find_elements_by_xpath('./div/div/div[2]/div/span')[0].text
    print(job_firm, job_name, job_money)
相关推荐
uyeonashi13 小时前
Boost搜索引擎测试报告
功能测试·selenium·搜索引擎
@zulnger15 小时前
selenium 自动化测试工具实战项目(订单)
selenium·测试工具·自动化
程序员三藏2 天前
selenium测试框架快速搭建
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
不会代码的小测试2 天前
UI自动化-日期插件的选择-可输入不可输入情况
javascript·vue.js·python·selenium·自动化
月明长歌2 天前
个人测试项目:一个可跑、可扩、可复用的 Selenium UI 自动化博客系统全链路测试拆解
selenium·ui·自动化
0思必得03 天前
[Web自动化] 反爬虫
前端·爬虫·python·selenium·自动化
不会代码的小测试3 天前
UI自动化-针对验证码登录的系统,通过首次手动登录存储cookie的方式后续访问免登录方法
开发语言·python·selenium
0思必得04 天前
[Web自动化] 数据抓取、解析与存储
运维·前端·爬虫·selenium·自动化·web自动化
0思必得04 天前
[Web自动化] 爬虫之API请求
前端·爬虫·python·selenium·自动化
0思必得04 天前
[Web自动化] 爬虫之网络请求
前端·爬虫·python·selenium·自动化·web自动化