【问题标题】:Python Selenium iterating through table and click correspondingly each rowPython Selenium 遍历表并相应地单击每一行
【发布时间】:2020-05-08 19:29:16
【问题描述】:

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

我应该在for row in table: 之后在哪里添加row 才能正确触发?

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()

【问题讨论】:

  • for row in table:(在循环体中)您似乎没有使用“行”,您使用的是静态 xPath,在每个循环中都不会更新?
  • 该表有很多行,在本例中为 5 行。我需要为每一行下载一个文件,但我只得到第一行文件。我应该添加/删除什么?

标签: python selenium


【解决方案1】:

我更喜欢 BeautifulSoup,而不是使用 selenium 来下载文件。 将您的表格更改为以下表格,以获取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

【讨论】:

  • 感谢您的帮助,但是,我所有的代码都在 selenium 上运行。在使用 selenium 顺利运行它之后,我假装使用 BS4,但是,现在,我需要使用 selenium 下载,这里只缺少一个步骤,我无法弄清楚。
  • 好吧,如果我理解正确的话,你只需要使用 Beautiful Soup 作为文件下载部分,剩下的你必须使用 selenium,这是最简单干净的解决方案。
  • 我有另一个代码从同一网站提取文件,但过滤其他文档。我用点击按钮下载部分的代码行编辑了我的问题
【解决方案2】:

试试下面的代码,这将定位到你之后的行。

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')

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 2016-06-05
相关资源
最近更新 更多