首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Selenium遍历表格并相应地单击每一行

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

Stack Overflow用户
提问于 2020-05-09 03:29:16
回答 2查看 139关注 0票数 0

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

我应该在for row in table:之后的哪里包含row才能正确地初始化?

代码语言:javascript
复制
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')

编辑

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

代码语言:javascript
复制
                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()
EN

Stack Overflow用户

回答已采纳

发布于 2020-05-09 04:49:31

尝试下面的代码,这将指向您要查找的行。

代码语言:javascript
复制
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')
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61686680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档