当前位置: 首页 > news >正文

第六:selenium鼠标操作和js代码执行

一.鼠标操作

1.鼠标是通过底层接口执行的,需要调用ActionChains对象来执行对应的方法
2.鼠标操作实现方式
2.1.在selenium中将操作鼠标的方法封装在ActionChains类 中,实例化对象action=ActionChains(driver)
2.1.1.context_click(element)右击-->模拟鼠标右键点击效果2.1.2.double_click(element)双击-->模拟鼠标双击效果2.1.3.drag_and_drop(source,target)拖动-->模拟鼠标拖动效果2.1.4.move_to_element(element)悬停-->模拟鼠标悬停效果2.1.5.perform()执行-->此方法用来执行以上所有鼠标操作
2.2.selenium提供鼠标操作的方法及步骤
2.2.1.需要导入ActionChains类2.2.2.通过ActionChains实例化鼠标对象 action=ActionChains(driver)# driver表示的是浏览器驱动对象2.2.3.调用鼠标的事件方法2.2.4.调用鼠标的执行方法 action.perform()
3.clickAndHold单击(不释放)
importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path='./chromedriver')asdriver:# 打开本地文件中的html文件driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')# click_and_hold 点击且不松开div=driver.find_element_by_xpath('//div[@onmousedown="mDown(this)"]')webdriver.ActionChains(driver).click_and_hold(div).perform()time.sleep(2)
4.context_click单击
importtimefromseleniumimportwebdriverfromselenium.webdriverimportActionChains dr=webdriver.Chrome()url=r'http://www.baidu.com'dr.get(url)ipt=dr.find_element_by_id('kw')action=ActionChains(dr)action.context_click(ipt)# 在输入框中右键操作action.perform()time.sleep(3)dr.quit()
5.double_click双击
importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path='./chromedriver')asdriver:# 打开本地文件中的html文件driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')# double_click 双击button=driver.find_element_by_xpath('//button[@ondblclick]')webdriver.ActionChains(driver).double_click(button).perform()time.sleep(2)
6.drag_and_drop拖动
6.1.此方法首先在源元素上单击并按住,然后移动到目标元素的位置后释放鼠标
6.2.调用鼠标拖动事件方法 action.drag_and_drop(source,target)# source表示的是源元素,被拖动的元素, target表示是目标源,也就是要拖动到哪个元素上
importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path='./chromedriver')asdriver:# 打开本地文件中的html文件driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')div1=driver.find_element_by_id('draggable')div2=driver.find_element_by_id('droppable')webdriver.ActionChains(driver).drag_and_drop(div1,div2).perform()time.sleep(3)
7.move_to_element悬停
7.1.此方法将鼠标移到元素的中间,执行此操作时,该元素也会滚动到视图中(悬停)
importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path='./chromedriver')asdriver:# 打开本地文件中的html文件driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')# move_to_elementdiv=WebDriverWait(driver,timeout=3).until(EC.visibility_of_element_located(('xpath','//div[@onmouseover="mOver(this)"]')))# 移动到# action=webdriver.ActionChains(driver)# action.move_to_element(div).perform()webdriver.ActionChains(driver).move_to_element(div).perform()time.sleep(2)
8.drag_and_drop_by_offset单元素拖动
8.1.action.drag_and_drop_by_offset(element,x,y)x,y 表示的元素拖动时横向和纵向移动的距离,单位为像素8.2.element表示的是元素对象 移动的像素最终要比在web页面中看到的移动像素值要大,最好大于5个像素或者10像素
importtimefromseleniumimportwebdriverfromselenium.webdriverimportActionChains dr=webdriver.Chrome()url=r'file:///C:/Users/tang/Desktop/pagetest/%E9%AA%8C%E8%AF%81%E7%A0%81/index.html'dr.get(url)h=dr.find_element_by_css_selector('.handler')action=ActionChains(dr)action.drag_and_drop_by_offset(h,260,0)# 单元素拖拽 滑动验证action.perform()time.sleep(5)dr.quit()
9.moveByOffset
9.1.此方法将鼠标从其当前位置(0,0)移动给定的偏移量,如果坐标在视图窗口之外,则鼠标最终将在浏览器窗口之外
importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path='./chromedriver')asdriver:# 打开本地文件中的html文件driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')# 移开 move_by_offsetwebdriver.ActionChains(driver).move_by_offset(xoffset=500,yoffset=500).perform()time.sleep(2)
10.release(释放)
10.1.此操作将释放按下的鼠标左键,如果WebElement转移了,它将释放给定WebElement上按下的鼠标左键
importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path='./chromedriver')asdriver:# 打开本地文件中的html文件driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')# release 松开鼠标webdriver.ActionChains(driver).release(div).perform()time.sleep(2)
11.鼠标链式操作
fromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome()asdriver:driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')# 创建一个动作链action=webdriver.ActionChains(driver)# move_to_elementdiv1=WebDriverWait(driver,timeout=3).until(EC.visibility_of_element_located(('xpath','//div[@onmouseover="mOver(this)"]')))# 指定等待时间为2saction.move_to_element(div1).pause(2)# 移开 move_by_offsetaction.move_by_offset(xoffset=500,yoffset=500).pause(2)# click_and_hold 点击且不松开div2=driver.find_element_by_xpath('//div[@onmousedown="mDown(this)"]')action.click_and_hold(div2).pause(2)# release 松开鼠标action.release(div2).pause(2)# 执行动作链action.perform()# 以上代码可重合在一起执行action.move_to_element(div1).pause(2).move_by_offset(xoffset=500,yoffset=500).pause(2).click_and_hold(div2).pause(2).release(div2).pause(2).perform()

二.执行js代码

1.selenium执行js有几个方法,通常使用 execute_script方法
2.页面滚动指定距离
importtimefromseleniumimportwebdriver ​withwebdriver.Chrome()asdriver:driver.get('https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%CD%BC%C6%AC&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111')# 滚动100px 100表示滚动条距离页面顶部100像素 0是x方向,100是y方向driver.execute_script("window.scrollTo(0,100)")time.sleep(1)# 200表示滚动条距离页面顶部200像素driver.execute_script("window.scrollTo(0,200)")time.sleep(1)driver.execute_script("window.scrollTo(0,300)")time.sleep(3)# 移动到底部driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")time.sleep(3)# 移动到顶部driver.execute_script("window.scrollTo(0,0)")time.sleep(3)
3.页面滚动至指定位置
3.1.执行js时,可以传递参数给js脚本
3.2.案例:打开页面,滚动到指定的元素可见
importtimefromseleniumimportwebdriver ​withwebdriver.Chrome(executable_path='./chromedriver')asdriver:driver.get('file:///Users/superfan/工作/myproject/study/po/scroll.html')time.sleep(2)div=driver.find_element_by_xpath('//div')# 移动到元素的底端与当前窗口的底部对齐driver.execute_script("arguments[0].scrollIntoView(false);",div)# arguments[0]表示传入第一个值div,arguments[1]表示传入第一个值div2# driver.execute_script("arguments[0].scrollIntoView(false);alert(arguments[1]);", div,1)time.sleep(2)# 移动到元素的顶端与当前窗口的顶端对齐driver.execute_script("arguments[0].scrollIntoView();",div)time.sleep(2)
http://www.jsqmd.com/news/993249/

相关文章:

  • 083、ASFF 自适应空间特征融合:Level 0/1/2 自学习融合权重的 Softmax 实现
  • 兰州保险纠纷维权指南:专业律师帮你打破理赔困局 - 云间寄笔
  • 2026年电动伸缩门厂家实力推荐:厦门杰特力金属制品有限公司多品类供应 - 品牌推荐官
  • 2026年6月最新|电力开关柜成套设备厂家推荐哪家好?国内口碑厂家排名与对比分析 - 商业新知
  • 武汉黄金回收机构实测|2026 最新行情与靠谱正规机构盘点 - 速递信息
  • ZDT_Emm42_V5.0驱动板Modbus-RTU通讯实战:从校准编码器到多机同步,一个Python脚本搞定
  • 专业干货!AI写专著工具推荐,助力20万字专著快速生成!
  • MPC853T硬件时序深度解析:从建立保持时间到CPM接口实战
  • 广州包包回收报价天花板,香奈儿 / 爱马仕等高价收 - 讯息早知道
  • 2026年执行律师深度选型指南:如何为你的胜诉债权匹配最佳方案? - 资讯速览
  • 082、BiFPN 加权特征金字塔:Fast Normalized Fusion 的加权方式与标准 FPN 的精度对比
  • 20张手绘图+收藏!小白程序员轻松看懂AI核心概念,从神经网络到Agent
  • YimMenu架构深度解析:从插件机制到安全实践的技术实现
  • 2026沈阳黄金回收防坑十策:附6家经过20项细节考核的店铺 - 奢侈品回收评测
  • 撬装装置优质厂家推荐:威海化工机械 —— 高端集成装备标杆 - 玖叁鹿
  • tebentafusp替本福司治葡萄膜黑色素瘤,细胞因子释放综合征需住院阶梯给药
  • MATLAB版最小二乘支持向量机全流程工具箱:含核函数、调参、去噪与多分类
  • 北京复印机租赁哪家靠谱|2026 权威实测榜单 黑白彩色复印机租赁推荐 - 商业观察
  • Simulink环境下基于EKF的车辆坡度与总质量在线联合估算模型(含误差对比与接口说明)
  • 2026图片去水印方法大全:免费工具、电脑软件、手机APP教程
  • 五个主流 AI 模型跑同一个任务,谁的返工率最低?
  • Arduino项目实战:用RGB三色灯DIY一个桌面情绪氛围灯(附完整源码)
  • 深入解析PCA9672 I2C I/O扩展器:从准双向口到中断应用实战
  • 2026上海黄金回收门店服务效率对比:实测结果公示 - 奢侈品回收评测
  • PMSM控制中的MTPA曲线及电机的弱磁控制
  • DLSS Swapper终极指南:免费开源工具一键智能切换游戏DLSS版本
  • 2026年吴忠全屋定制装修公司选择指南:新视野装饰vs行业五大品牌深度横评 - 优质企业观察收录
  • 模型评测体系:大模型输出一致性评估与自动化回归测试
  • 鸿蒙原生应用实战(一):项目初始化与首页仪表盘开发
  • 斯皮尔曼相关系数实战:从单调关系到数据洞察