[我正在尝试使用以下代码在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
这是我第一次使用硒。
您尝试过
对于范围内的选项(len(all_options [:267])):?
您可以尝试将all_options的元素列表分配给列表,以使迭代起作用。然后在for循环中读取元素。
all_options = []
要使用https://www10.goiania.go.gov.br/TransWeb/FuncionariosExportarPopUp.aspx?_=1590514086637和Selenium从python站点下载多个文件,请从Referência drop-down-menu中选择每个选项,您需要为WebDriverWait引入element_to_be_clickable()
,并且可以使用跟随Locator Strategies:
代码块:
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