如何在 python 中使用 Selenium 从选择下拉列表的每个选项的站点下载多个文件

How to download multiple files from a site selecting each option of a dropdown using Selenium in python

我正在尝试使用以下代码从 python 中使用 Selenium 的站点下载多个文件。

from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome('chromedriver.exe')
driver.maximize_window()
driver.get('https://www10.goiania.go.gov.br/TransWeb/FuncionariosExportarPopUp.aspx?_=1590514086637')
element = driver.find_element_by_id('WebPatterns_wt12_block_wtMainContent_wtcboReferencia')
all_options = element.find_elements_by_tag_name("option")
selectYear = Select(driver.find_element_by_id("WebPatterns_wt12_block_wtMainContent_wtcboReferencia"))
link = driver.find_element_by_id('WebPatterns_wt12_block_wtMainContent_wtbtnGerar')
for option in all_options[:267]:
    print("Value is: %s" % option.get_attribute("value"))
    selectYear.select_by_value(option)
    link.click()
    time.sleep(5000)

但是我遇到了这个错误,我不知道如何解决。

TypeError: argument of type 'WebElement' is not iterable

这是我第一次使用 selenium。

你试过吗?

for option in range(len(all_options[:267])):

您能否尝试将 all_options 的元素列表分配给列表,以便迭代到 work.Then 读取 for 循环中的元素。

all_options = []

从站点下载多个文件 https://www10.goiania.go.gov.br/TransWeb/FuncionariosExportarPopUp.aspx?_=1590514086637 using and selecting each option from the Referência you need to induce for the element_to_be_clickable() and you can use the following :

  • 代码块:

    driver.get("https://www10.goiania.go.gov.br/TransWeb/FuncionariosExportarPopUp.aspx?_=1590514086637")
    select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='WebPatterns_wt12_block_wtMainContent_wtcboReferencia']"))))
    for opt in select.options:
        opt.click()
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Gerar']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import Select