Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium scroll by specific pixel values

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.

like image 993
user2661518 Avatar asked Jul 31 '18 20:07

user2661518


People also ask

How do I scroll up and down in Selenium?

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.

How do I scroll vertically in Selenium?

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.


1 Answers

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.

like image 95
PixelEinstein Avatar answered Sep 30 '22 20:09

PixelEinstein