Python 3.x 如何在python中使用selenium chromedriver水平滚动
我正在使用Python中的Selenium Chromedriver进行web抓取。我的链接如下: 我想在链接中获取表的数据。但是这个表有一个滚动条,所以我只能在表的可见列中获取数据。我想得到表中的所有数据 ex)Python 3.x 如何在python中使用selenium chromedriver水平滚动,python-3.x,selenium,web-scraping,Python 3.x,Selenium,Web Scraping,我正在使用Python中的Selenium Chromedriver进行web抓取。我的链接如下: 我想在链接中获取表的数据。但是这个表有一个滚动条,所以我只能在表的可见列中获取数据。我想得到表中的所有数据 ex) 驱动程序。通过_css_选择器(“#columntablejqxgrid”)查找_元素。文本 (我的目标) (结果) 因此,我必须水平滚动表格并获取其余数据。如何实现这一点?对于滚动,可以使用ActionChains类 from selenium.webdriver.common.a
驱动程序。通过_css_选择器(“#columntablejqxgrid”)查找_元素。文本
(我的目标)
(结果)
因此,我必须水平滚动表格并获取其余数据。如何实现这一点?对于滚动,可以使用ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
然后你可以用类似的方法移动滑块
ActionChains(driver).click_and_hold(slider).move_by_offset(x , 0).release().perform()
以下是一个适用于您的案例的有效解决方案:
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()
输出
['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']
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()
['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']