简体   繁体   中英

Scroll element horizontally - Selenium Python

Let's say I want to scroll a scrollbar in some element. As an example, let's take the link "https://www.w3schools.com/howto/howto_css_table_responsive.asp". If I run:

element = driver.find_element_by_xpath("//table")

I get the table element. But now, I want to scroll the scrollbar horizontally, that is controling the view of this table. How can I do this? I already tried something like:

driver.execute_script("arguments[0].scrollLeft = 200;",element)

But I wasn't successfull. I also tried sending keys, but it didn't work too.

You do not need to actually be able to see elements for Selenium to target them.

Just proceed to target the elements that are 'off screen' as you would if they were on screen.

Selenium is targeting the DOM - which as a loose analogy which I will probably be roasted for in the comments is like a screen with infinite length and width - so no scrolling necessary - it (selenium) can already "see" everything 'on' that 'infinite screen'

If you are having trouble selecting specific elements from within the table in question (if you need a specific code snippet) please feel free to comment - I am rather familiar with Selenium.

EDIT:

In your specific case, since they are being loaded dynamically you are 100% correct that you will need to scroll to get the data. Let's take care of vertical scrolling by hitting the More Financial Data button at the bottom that expands the list.

Now for scrolling to the right - since we can just scroll all the way to the right and have all data that we need to access visible (assuming you don't have a Premium account).

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
url = "https://www.morningstar.com/stocks/xnas/tsla/financials"
driver.get(url)
driver.maximize_window()
# Click on Income Statement
xpath = '//*[@id="__layout"]/div/div[2]/div[3]/main/div[2]/div/div/div[1]/sal-components/section/div/div/div/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div[2]/div[2]/div[1]/div[1]/a'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath))).click()
# click on More Financials Detail Data
xpath = '//*[@id="__layout"]/div/div[2]/div[3]/main/div[2]/div/div/div[1]/sal-components/section/div/div/div/div/div[2]/div/div[2]/div/div[2]/div/div[3]/div[2]/div/a'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath))).click()

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

xpath='/html/body/div[2]/div/div/div[2]/div[3]/main/div[2]/div/div/div[1]/sal-components/section/div/div/div/div/div[2]/div/div[2]/div/div[2]/div/div[2]/div[4]/div/div[3]/div[2]/div[2]'
horizontal_bar_width = driver.find_element_by_xpath(xpath).rect['width']
slider = driver.find_element_by_xpath(xpath)
ActionChains(driver).click_and_hold(slider).move_by_offset(horizontal_bar_width/2, 0).release().perform()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM