Python Selenium遍历表格并相应地单击每一行

问题描述 投票:0回答:2

我正在尝试遍历表并下载xml文件,但是,我只下载表中第一个元素的内容。如何正确迭代从每一行下载内容?

row之后我应该在哪里包括for row in table:以正确地重新计算?

from selenium import webdriver
options.add_argument("--incognito")
driver = webdriver.Chrome(options=options)

driver.get('https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=30983020000190')
driver.find_element_by_css_selector(f'input[type="search"]').click()
driver.find_element_by_css_selector(f'input[type="search"]').send_keys('rendimentos')
time.sleep(1)
table = driver.find_elements_by_xpath("//table[@id='tblDocumentosEnviados']//tr")
for row in table:
try:
WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH,"//table[@id='tblDocumentosEnviados']//td[text()='Rendimentos e Amortizações']/following-sibling::td[.//span[text()='Ativo']]/following-sibling::td//a[@title='Download do Documento']"))).click()
x = x + 1
print(x)
except:
print('except')

编辑

我需要在此行中添加行迭代才能成功:

                try:
                    WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH,
                                                                               "//table[@id='tblDocumentosEnviados']//td[text()='Rendimentos e Amortizações']/following-sibling::td[.//span[text()='Ativo']]/following-sibling::td//a[@title='Download do Documento']"))).click()
python selenium
2个回答
1
投票

尝试下面的代码,这将针对您想要的行。

options.add_argument("--incognito")
driver = webdriver.Chrome(options=options)

driver.get('https://fnet.bmfbovespa.com.br/fnet/publico/abrirGerenciadorDocumentosCVM?cnpjFundo=30983020000190')
driver.find_element_by_css_selector('input[type="search"]').click()
driver.find_element_by_css_selector('input[type="search"]').send_keys('rendimentos')
time.sleep(1)
table = driver.find_elements_by_xpath("//table[@id='tblDocumentosEnviados']//tr")
print(len(table))
for row in range(len(table)):
   try:
      WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH,"//table[@id='tblDocumentosEnviados']//tr[" + str(row) + "]//td[text()='Rendimentos e Amortizações']/following-sibling::td[.//span[text()='Ativo']]/following-sibling::td//a[@title='Download do Documento']"))).click()
      x = row + 1
      print(x)
   except:
      print('except')

3
投票

而不是使用硒来下载文件,我更喜欢BeautifulSoup。将您的表更改为下面的表,以获取html

from bs4 import BeautifulSoup
table = driver.find_elements_by_xpath("//table[@id='tblDocumentosEnviados']")

table_html = table[0].get_attribute('outerHTML')
table_html = BeautifulSoup(table_html, 'lxml')
list_url = []

for tr in table_html.find_all('tr'):
    for td in tr.find_all('td'):
        file_anchor = td.find('a', {'title': 'Download do Documento'})
        if file_anchor:
            complete_url = 'https://fnet.bmfbovespa.com.br/fnet/publico/{}'.format(file_anchor.get('href'))
            list_url.append(complete_url)

现在您可以使用request.get下载文件,希望对您有所帮助!

文件下载-https://www.tutorialspoint.com/downloading-files-from-web-using-python

© www.soinside.com 2019 - 2024. All rights reserved.