selenium scroll by specific pixel values

梦想与她 提交于 2021-01-27 05:20:27

问题


I see for python selenium I can scroll to element using

self.driver.execute_script("return arguments[0].scrollIntoView(true);", element)

Or using pixel value as

driver.execute_script("window.scrollTo(0, <vertical_position_to_scroll> )")

But is there way to scroll by specific pixel values from current element or current position. e.g. If I moved to element I want to scroll 10 pixel up from there.


回答1:


I would use window.scrollBy(x, y)

like this:

#first move to the element
self.driver.execute_script("return arguments[0].scrollIntoView(true);", element)
#then scroll by x, y values, in this case 10 pixels up
self.driver.execute_script("window.scrollBy(0, -10);")

HERE you will find the documentation on scrollBy.




回答2:


to scroll to a specific element on a page:

element = driver.find_element_by_xpath("html/body/div[6]/div[1]/div[2]/a")

element.location_once_scrolled_into_view


来源:https://stackoverflow.com/questions/51621817/selenium-scroll-by-specific-pixel-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!