playwright实现鼠标拖动滑块功能

Python与SEO 思享 667浏览

思路说明

  1. 使用locator定位到要拖动滑块元素,如元素名叫ele
  2. 获取元素ele的bounding_box含4分属性值:x,y,width,height
  3. 把鼠标移动到元素ele的中心点,中心点位置为:x+width/2,y+height/2
  4. 按下鼠标
  5. 计算出要移动的下一个位置,以长条滑块为例,拖动到长条头部实现解锁,那x的位置应该为x+width/2 + 某个固定值(足够大就好)
  6. 执行移动操作,下一个位置坐标为:x+width/2 + 某个固定值,y+height/2

调用方法

  • 元素定位:page.locator()
  • 获取元素位置及大小:ele.bounding_box()
  • 鼠标移动:page.mouse.move()
  • 按下鼠标:page.mouse.down()
  • 释放鼠标:page.mouse.up()

示例代码

dropbutton=page.locator("#loginForm > div:nth-child(12) > div > div.drag-btn")
box=dropbutton.bounding_box()
page.mouse.move(box['x']+box['width']/2,box['y']+box[ 'height']/2)
page.mouse.down()
mov_x=box['x']+box['width']/2+260
page.mouse.move(mov_x,box['y']+box[ 'height']/2)
page.mouse.up()

推荐阅读

playwright可以仿真哪些移动设备?

我们知道selenium可以通过模拟UA实现模拟手机。而playwright更是自带对于移动设备的模拟。那么我们怎么知道都可以模拟哪些设备呢? 代码示例 from playwright.sync_api import Playwright, sync......

Playwright新窗口被检测webdriver的解决办法

之前有转载了一篇文章,关于《Playwright怎么绕过webdriver检测》但是在使用过程中会遇到一个问题,就是打开新的窗口,window.navigator.webdriver参数会为true,也就是说绕过webdriver仅限单窗口,那么,该......

Playwright怎么绕过webdriver检测

Playwright是微软开发的自动化测试工具,支持近乎目前市面上绝大部分的浏览器。 但是由于是新生产物,所以很多细节可能还不如Selenium操作起来比较顺手。 例如,使用Selenium打开网页时,可以使用execute_cdp_cmd在load......