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

≯℡__Kan透↙ 提交于 2021-02-04 08:09:29

问题


I am trying to download multiple files from a site using Selenium in python using the following code.

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)

But i'm getting this error and i do not know how to solve it.

TypeError: argument of type 'WebElement' is not iterable

This is the first time that i am using selenium.


回答1:


To download multiple files from the site https://www10.goiania.go.gov.br/TransWeb/FuncionariosExportarPopUp.aspx?_=1590514086637 using Selenium and python selecting each option from the Referência drop-down-menu you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • Code Block:

    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()
    
  • Note : You have to add the following imports :

    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
    



回答2:


Can you try assigning the list of elements for all_options to a list, for iteration to work.Then read the elements in for loop.

all_options = []




回答3:


did you try this?

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


来源:https://stackoverflow.com/questions/62029058/how-to-download-multiple-files-from-a-site-selecting-each-option-of-a-dropdown-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!