Hamed Baziyad
Hamed Baziyad

Reputation: 2019

Scrolling task of Selenium is not worked for me in Python environment

I want to write a script with adjustable speed. I want to scroll the page into a specific height of the page. I used window.scrollTo as follows:

driver.execute_script("window.scrollTo(0, 10)")

Although there is no error, after running, we cannot see any change in scroll position. Could you help me, please?

Upvotes: 0

Views: 93

Answers (2)

Hamed Baziyad
Hamed Baziyad

Reputation: 2019

The problem was solved for me by the code as follows:

src = driver.find_element_by_xpath("/html/body/div/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div[2]/div")

driver.execute_script("arguments[0].scrollTop = (0,10)", src)

Upvotes: 2

Prophet
Prophet

Reputation: 33351

I think your code is correct, but since you are using too short difference, trying to scroll for 10 pixels only, it's not visible.
Please try giving more significant height, like 100 or more and see if it works.
Or put your script in a loop to make the result more noticeable.

Upvotes: 0

Related Questions