Я занимаюсь парсингом веб-страниц с помощью Selenium Chromedriver в Python. Теперь для сетки (с возможностью горизонтальной прокрутки), когда я пытаюсь получить данные, я получаю только до видимой части сетки в браузере.
Например, здесь я могу получать данные только до Part Category
, вот так
['', '', '', '', '', 'Item Number', 'Item Description', 'Lifecycle Phase', 'Old Lifecycle Phase', 'Docs Rqd', 'PGDS Audit', 'Part Category', '', '', '', '', '', '', '', '', '', '', '', '', '']
, хотя есть еще несколько столбцов, существует.
Я пробовал actions.move_to_element
, driver.execute_script
, но не работал.
Вот мой пример кода
for i in range(len(titles)):
current_tab = driver.find_elements_by_xpath("//div[@id='tabsDiv']/ul/li/a")[i:i+1]
current_tab_name=current_tab[0].text
current_tab[0].click()
time.sleep(5)
if (current_tab_name=='Affected Items'):
current_tab_info=driver.find_elements_by_xpath("//div[@class='GMHeadMid']/table[@class='GMSection']/tbody/tr[@class='GMHeaderRow']/td") ## this is the scroll-able grid
driver.execute_script("window.scrollTo(0, 100)")
#current_tab_info[0].location_once_scrolled_into_view
#actions = ActionChains(driver)
#actions.move_to_element(current_tab_info[0]).perform()
current_tab_header_list=[x.text for x in current_tab_info]
print(current_tab_header_list)
Почему бы вам не заставить драйвер прокручиваться к элементу вместо горизонтальной прокрутки?
scrollToElement = "arguments[0].scrollIntoView()"
driver.execute_script(scrollToElement, current_tab_info)
Я не ожидаю от питона, поэтому в моем синтаксисе может быть что-то не так.
Наконец-то я нашел обходной путь
# First of all get all the header column span IDs
current_tab_info = driver.find_elements_by_xpath("//div[@class='GMHeadMid']/table[@class='GMSection']/tbody/tr[@class='GMHeaderRow']/td/div/span")
current_tab_header_list = [x.get_attribute('id') for x in current_tab_info]
# Then get element text against each ID
current_tab_header_label_list =[]
for i in current_tab_header_list:
# will scroll until that element is not appeared on page
current_header_info = driver.find_elements_by_xpath(
"//div[@class='GMHeadMid']/table[@class='GMSection']/tbody/tr[@class='GMHeaderRow']/td/div/span[@id='"+str(i)+"']")
driver.execute_script("arguments[0].scrollIntoView(true);", current_header_info[0])
current_tab_header_label_list.append(current_header_info[0].text)
Да, пробовал, но не повезло. По-прежнему получаются только видимые столбцы