Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

scroll to element selenium python

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

driver_service = Service(executable_path="C:/selenium_browser_driver/chromedriver.exe")
driver = webdriver.Chrome(service=driver_service)
site = 'https://example.com'
driver.get(site)  # opens the site in browser

element = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.XPATH, '//div[@class="perf-chart-container"]')))
#element = driver.find_element(By.XPATH, '//div[@class="perf-chart-container"]')
desired_y = (element.size['height'] / 2) + element.location['y']
current_y = (driver.execute_script('return window.innerHeight') / 2) + driver.execute_script('return window.pageYOffset')
scroll_y_by = desired_y - current_y
driver.execute_script("window.scrollBy(0, arguments[0]);", scroll_y_by)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #scroll #element #selenium #python
ADD COMMENT
Topic
Name
9+7 =