Can anyone help me with selenium python code for Scrolling Horizontal and vertical bar of a webpage [duplicate]

I am doing web-scraping using Selenium Chromedriver in Python. My link is below: https://www.macrotrends.net/stocks/charts/AAPL/apple/balance-sheet

I want to get data of table in the link. But The table has a scrollbar so I can only get the data in visible columns of the table. I want to get all data of table.

ex)

driver.find_element_by_css_selector('#columntablejqxgrid').text

My goal)

Annual Data | Millions of US $ except per share data
2019-09-30
2018-09-30
2017-09-30
2016-09-30
2015-09-30
2014-09-30
2013-09-30
2012-09-30
2011-09-30
2010-09-30
2009-09-30
2008-09-30
2007-09-30
2006-09-30
2005-09-30

The result)

Annual Data | Millions of US $ except per share data
2019-09-30
2018-09-30
2017-09-30
2016-09-30
2015-09-30
2014-09-30

So, I have to scroll the table horizontally and get the rest of data. How can I achieve that?


Solution 1:

For the scrolling you can use the ActionChains Class

from selenium.webdriver.common.action_chains import ActionChains

Then you can move the slider using somthing like

ActionChains(driver).click_and_hold(slider).move_by_offset(x , 0).release().perform()

Here's a working solution for you case:

horizontal_bar_width = driver.find_element_by_id('jqxScrollOuterWraphorizontalScrollBarjqxgrid').rect['width']
slider = driver.find_element_by_id('jqxScrollThumbhorizontalScrollBarjqxgrid')
    header_columns = []

# Using iteration as span values won't show went they are hidden
for _ in range(4):
    for el in driver.find_elements_by_css_selector('#columntablejqxgrid [role="columnheader"] span'):
        if el.text not in header_columns and el.text != '':
            header_columns.append(el.text)
    # Ensure the slider is in view
    slider.location_once_scrolled_into_view
    ActionChains(driver).click_and_hold(slider).move_by_offset(horizontal_bar_width/4, 0).release().perform()

Output

['Annual Data | Millions of US $ except per share data',
 '2019-09-30',
 '2018-09-30',
 '2017-09-30',
 '2016-09-30',
 '2015-09-30',
 '2014-09-30',
 '2013-09-30',
 '2012-09-30',
 '2011-09-30',
 '2010-09-30',
 '2009-09-30',
 '2008-09-30',
 '2007-09-30',
 '2006-09-30',
 '2005-09-30']