ClericPy / ichrome

Chrome controller for Humans, based on Chrome Devtools Protocol(CDP) and python3.7+.
https://pypi.org/project/ichrome/
MIT License
228 stars 29 forks source link

有没有模拟鼠标滚动相关的活动 #60

Closed lalalatutu closed 3 years ago

lalalatutu commented 3 years ago

如题

ClericPy commented 3 years ago

如果 JS 有那就会有 至于默认 cdp 上的话, 搜索关键词 scroll 时候有几个相关的 https://chromedevtools.github.io/devtools-protocol/

具体还是看 case, 对 Chrome 浏览器自动化来说, 能 JS 的就走原生 JS, 其次考虑 cdp 里支持的情况, 多数情况还是用 JS 的 Element.scrollIntoView() - Web APIs | MDN 比较常见

bluebad commented 2 years ago

Input.dispatchMouseEvent有个mouseWheel,实际使用却无法滚动,不知道什么原因。

ClericPy commented 2 years ago

那个我没有用过也不确定你是怎么用的, 用途是什么啊, 我平时滚动都走 JS 了 你这随便说几句我也不知道怎么复现出来...

bluebad commented 2 years ago
await current_tab.send('Input.dispatchMouseEvent',
                        type="mouseWheel",
                        timeout=2,
                        deltaY=int(round(50)))

这样写的,没看到页面有滚动,本意是让滚动条向下滚动50px。

ClericPy commented 2 years ago

我找个页面试试哈

bluebad commented 2 years ago
await current_tab.send('Input.dispatchMouseEvent',
                        type="mouseWheel",
                        timeout=2,
                        deltaY=int(round(50)))

这样写的,没看到页面有滚动,本意是让滚动条向下滚动50px。

这个滚动条是整个页面的滚动条

ClericPy commented 2 years ago

看了下返回值是报错了 {'id': 1, 'error': {'code': -32602, 'message': 'Invalid parameters', 'data': 'Failed to deserialize params.x - BINDINGS: mandatory field missing at position 40'}} 这个报错没搜到什么意思...

为啥不用 JS 做滚动啊 await tab.js(r'''window.scrollTo( 0, 1000 )''')

ClericPy commented 2 years ago

看明白报错了... 忘了放 x y 参数了

ClericPy commented 2 years ago
            print(await tab.send('Input.dispatchMouseEvent',
                                 type="mouseWheel",
                                 timeout=2,
                                 x=0,
                                 y=0,
                                 deltaX=0,
                                 deltaY=100))
  1. 要注意非 Optional 的参数不能空着
  2. 要注意看返回的报错是什么
  3. JS 才是一等公民
bluebad commented 2 years ago

啊啊啊,明白了,原来还可以这样print结果,受教了。非常感谢。

ClericPy commented 2 years ago

怪我不喜欢写文档... 祝使用愉快

bluebad commented 2 years ago

开源不易,怪我没多看看源码。

怪我不喜欢写文档... 祝使用愉快