2

If I want to scroll to the end of a page I use the following:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

What's the equivalent for scrolling all the way to the right? My first guess was:

driver.execute_script("window.scrollTo(document.body.scrollWidth, 0);")

However, this didn't work and gave the following error:

JavascriptException}Message: javascript error: Cannot read properties of null (reading 'scrollWidth')

I only want the so-called golf green (the green circle on the right)

HJA24
  • 410
  • 2
  • 11
  • 33

1 Answers1

0

Instead of directional scrolling an easier approach would be to identify the desired element inducing WebDriverWait for the visibility_of_element_located() and invoke scrollIntoView() as follows:

element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "element_css")))
driver.execute_script("return arguments[0].scrollIntoView(true);", element)

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

You can find a relevant detailed discussion in Scrolling to top of the page in Python using Selenium


Update

To scrollIntoView the golf green (the green circle on the right) you can use the following solution:

driver.get(URL)
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "svg g path[fill='#92f100']")))
driver.execute_script("return arguments[0].scrollIntoView(true);", element)
driver.save_screenshot("green_circle_on_the_right.png")

Saved Screenshot:

green_circle_on_the_right.png

HJA24
  • 410
  • 2
  • 11
  • 33
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352