Issue
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)
URL = 'https://apps.frs-dev.imgarena.dev/golf/3d/courses/PGA-4/holeNo/13/map_preview.svg'
Solution
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("https://apps.frs-dev.imgarena.dev/golf/3d/courses/PGA-4/holeNo/13/map_preview.svg")
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:
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.