简体   繁体   English

水平滚动元素 - Selenium Python

[英]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".例如,让我们以链接“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.您实际上不需要能够看到 Selenium 的元素来定位它们。

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' Selenium 的目标是 DOM - 作为一个松散的类比,我可能会在评论中被烘烤,就像一个具有无限长和宽的屏幕 - 所以不需要滚动 - 它(selenium)已经可以“看到”一切“在” '无限屏幕'

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.如果您在从相关表中选择特定元素时遇到问题(如果您需要特定的代码片段),请随时发表评论 - 我对 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.在您的特定情况下,由于它们是动态加载的,因此您 100% 正确,您需要滚动才能获取数据。 Let's take care of vertical scrolling by hitting the More Financial Data button at the bottom that expands the list.让我们点击底部展开列表的More Financial Data按钮来处理垂直滚动。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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