如何使用Selenium[Python]在元素内实现平滑滚动

huangapple go评论73阅读模式
英文:

How to smooth scroll inside element with Selenium[Python]

问题

我正在使用以下代码来在一个元素内滚动(动态生成的列表):

driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", element)

有没有办法进行平滑滚动(而不是当前的逐步滚动)?感觉当它跳来跳去时,我可能错过了一些数据。

英文:

I'm using

driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", element)

in order to scroll inside an element ( dynamically generated list). The code above is the only one I found that actually works and scrolls.
Is there any way to do a smooth scroll (instead of the actual step scroll)? I feel like I'm missing some data when it's jumping around.

答案1

得分: 1

你可以使用 Element#scroll,它允许传递一个选项对象,在其中可以指定 behavior: 'smooth'

driver.execute_script("arguments[0].scroll({top: arguments[0].scrollHeight, behavior: 'smooth'})", element)
英文:

You can use Element#scroll which allows passing an options object in which behavior: 'smooth' can be specified.

driver.execute_script("arguments[0].scroll({top: arguments[0].scrollHeight, behavior: 'smooth'})", element)

huangapple
  • 本文由 发表于 2023年5月11日 07:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定