问题
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