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.
We can scroll the page up or down in Selenium webdriver using Java. This is achieved with the help of the Actions class. First of all, we have to create an object of this Actions class and then apply the sendKeys method to it. Now, to scroll down a page, we have to pass the parameter Keys.
Selenium can execute JavaScript commands with the help of the executeScript method. To scroll down vertically in a page we have to use the JavaScript command window. scrollBy. Then pass the pixel values to be traversed along the x and y axis for horizontal and vertical scroll respectively.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With