Получить ссылки выпадающего меню в каждой строке таблицы с python3 и selenium - PullRequest
0 голосов
/ 11 января 2019

Как начинающий питон, я хочу скачать старую газету, заархивированную на сайте (http://digesto.asamblea.gob.ni/consultas/coleccion/), с моим сценарием ниже.

Однако мне не удается заставить мой скрипт пройтись по каждой строке таблицы и выбрать «PDF» в раскрывающемся меню, сохранив соответствующую ссылку в списке (для их загрузки).

Кажется, моя проблема в том, что скрипт не может найти значение PDF из каждого выпадающего меню, используя предоставленный xpath.

Это просто часть исходного кода, которая не работает:

table_id = driver.find_element(By.ID, 'gridTableDocCollection')
rows = table_id.find_elements(By.TAG_NAME, "tr") # get all table rows
for row in rows:
    elems = driver.find_elements_by_xpath('//ul[@class="dropdown-menu"]/a')
    for elem in elems:
        print(elem.get_attribute("href"))

Изменить:

Когда я использую этот код:

list_of_links = driver.find_element_by_xpath('//ul[@class="dropdown-menu"]/li')
print(list_of_links)

Я получаю selenium.webdriver.firefox.webelement.FirefoxWebElement (session="e6799ba5-5f0b-8b4f-817a-721326940b91", element="66c956f0-d813-a840-b24b-a12f92e1189b" вместо ссылки. Что я делаю не так?

Может кто-нибудь помочь мне? Я часами читал через stackoverflow, но там, где никогда ничего не получалось (смотри часть кода, которая закомментирована).

Отказ от ответственности: при использовании скрипта вам нужно вводить капчу вручную, не нажимая Enter для продолжения скрипта.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# A small script to download issues of the Gaceta de Nicaragua (1843-1960) 19758 issues

import logging
from selenium.webdriver.remote.remote_connection import LOGGER
LOGGER.setLevel(logging.WARNING)

import os
import sys
import time
import shutil
from subprocess import call
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.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains

profile = webdriver.FirefoxProfile() # profile to prevent download manager
profile.set_preference("network.cookie.cookieBehavior", 0) # accept all cookies
profile.set_preference("network.cookie.lifetimePolicy", 0) # accept cookies
profile.set_preference("network.cookie.alwaysAcceptSessionCookies", 1) # always allow sess
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'Downloads/')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'image/jpeg;application/jpeg;image/jpg;application/jpg')

url = 'http://digesto.asamblea.gob.ni/consultas/coleccion/' # web page
print('Opening digesto.asamblea.gob.ni...')

driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url) # open url

driver.find_element_by_xpath('//*[@id="cavanzada"]').click() # advanced menu

driver.find_element_by_xpath("//select[@id='slcCollection']/option[text()='Diario Oficial']").click()
driver.find_element_by_xpath("//select[@id='slcMedio']/option[text()='Gaceta Oficial']").click() # change journal name here

inputElement = driver.find_element_by_xpath('//*[@id="txtDatePublishFrom"]')
inputElement.send_keys('01/01/1844') # change start date

inputElement = driver.find_element_by_xpath('//*[@id="txtDatePublishTo"]')
inputElement.send_keys('31/12/1860') # change end date

time.sleep( 5 ) # wait for Human Captcha Insertion

inputElement.send_keys(Keys.ENTER) # search

time.sleep( 2 ) # wait to load

select_element = Select(driver.find_element_by_xpath('//*[@id="slcResPage"]')) # page count
select_element.select_by_value('50') # max 50

time.sleep( 1 ) # wait to load

list_of_links = driver.find_elements_by_xpath('//ul[@class="dropdown-menu"]/a')
print(list_of_links)

#a=[];
#a = driver.find_elements_by_link_text("PDF");
#driver.find_element_by_link_text("PDF").click()
#a = driver.find_element_by_xpath("//select[@class='dropdown-menu']/option[text()='PDF']").click()
#a = driver.find_element_by_xpath('//*[contains(text(), '"dropdown-menu"')] | //*[@#='"PDF"']'); #[contains(@#, "PDF")]
#a = driver.find_elements_by_xpath("//*[contains(text(), 'PDF')]")
#a = driver.find_elements_by_xpath('//div[@class="dropdown-menu"][contains(@#, "PDF")]')
#print(a, sep='\n')
#print(*a, sep='\n')

#driver.find_element(By.CssSelector("a[title='Acciones']")).find_element(By.xpath(".//span[text()='PDF']")).click();

#select_element = Select(driver.find_element_by_xpath('//*[@id="gridTableDocCollection"]/html/body/div[3]/div[1]/div/div/form/div[3]/div[2]/table/tbody/tr[1]/td[5]/div/ul/li[1]/a'))
#select_element.select_by_text('PDF')

table_id = driver.find_element(By.ID, 'gridTableDocCollection')
rows = table_id.find_elements(By.TAG_NAME, "tr") # get all table rows
for row in rows:
    elems = driver.find_elements_by_xpath('//ul[@class="dropdown-menu"]/a')
    for elem in elems:
        print(elem.get_attribute("href"))

1 Ответ

0 голосов
/ 11 января 2019

подумайте больше о шагах, которые вы выполните вручную сейчас вы начали цикл по всем строкам, но ничего не сделали с элементом row. вам нужно щелкнуть раскрывающийся список для строки, а затем выбрать параметр PDF

table_id = driver.find_element(By.ID, 'tableDocCollection')
rows = table_id.find_elements_by_css_selector("tbody tr") # get all table rows
for row in rows:
    # click on the button to get the dropdowns to appear
    row.find_element_by_css_selector('button').click()
    # now find the one that's the pdf (here, using the fact that the onclick attribute of the link has the text "pdf")
    row.find_element_by_css_selector('li a[onclick*=pdf]').click()

Отсюда вам нужно перейти в новое окно и скачать PDF. Попробуйте решить эту проблему, а затем, если вам нужна помощь, отправьте новый вопрос.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...