我正在構建一個網路爬蟲,它必須嘗試多個下拉選單選項的組合并從每個組合中收集資料。所以基本上有5個下拉選單。我必須從下拉選項的所有可能組合中收集資料。對于每個組合,我必須按下一個按鈕來拉出包含所有資料的頁面。我將所有資料存盤在字典中。
這是網站:http ://siops.datasus.gov.br/filtro_rel_ges_covid_municipal.php?S=1&UF=12;&Municipio=120001;&Ano=2020&Periodo=20
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# General Stuff about the website
path = '/Users/admin/desktop/projects/scraper/chromedriver'
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome(options=options, executable_path=path)
website = 'http://siops.datasus.gov.br/filtro_rel_ges_covid_municipal.php'
driver.get(website)
# Initial Test: printing the title
print(driver.title)
print()
# Dictionary to Store stuff in
totals = {}
# Drop Down Menus
year_select = Select(driver.find_element(By.XPATH, '//*[@id="cmbAno"]'))
uf_select = Select(driver.find_element(By.XPATH, '//*[@id="cmbUF"]'))
### THIS IS WHERE THE ERROR IS OCCURING ###
# Choose from the drop down menus
uf_select.select_by_value('29')
year_select.select_by_value('2020')
# Submit button on the page
submit_button = driver.find_element(By.XPATH, '//*[@id="container"]/div[2]/form/div[2]/div/input[2]')
submit_button.click()
# Pulling data from the webpage
nameof = driver.find_element(By.XPATH, '//*[@id="arearelatorio"]/div[1]/div/table[1]/tbody/tr[2]').text
total_balance = driver.find_element(By.XPATH, '//*[@id="arearelatorio"]/div[1]/div/table[3]/tbody/tr[9]/td[2]').text
paid_expenses = driver.find_element(By.XPATH, '//*[@id="arearelatorio"]/div[1]/div/table[4]/tbody/tr[11]/td[4]').text
# Update Dictionary with the new info
totals.update({nameof: [total_balance, paid_expenses]})
totals.update({'this is a test': ['testing stuff']})
# Print the final Dictionary and quit
print(totals)
driver.quit()
由于某種原因,此代碼在嘗試 1 種可能的組合時不起作用(從 UF 下拉串列中選擇值 29,以及從 year_select 下拉串列中選擇值 2020)。如果我從兩個下拉選擇中注釋掉,那么它作業得很好。
如何在單次迭代中嘗試多種下拉選項組合?
uj5u.com熱心網友回復:
試試這個。
# Drop Down Menus
### THIS IS WHERE THE ERROR IS OCCURING ###
# Choose from the drop down menus
uf_select = Select(driver.find_element(By.XPATH, '//*[@id="cmbUF"]'))
uf_select.select_by_value('29')
year_select = Select(driver.find_element(By.XPATH, '//*[@id="cmbAno"]'))
year_select.select_by_value('2020')
這對我有用。在你的例子中,我得到了一個陳舊的......錯誤,意味著元素消失了。還沒有檢查,但也許復選框以某種方式更新并在選擇另一個復選框時丟失了參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/529700.html
上一篇:SeleniumWeb驅動程式(driver.find_element(By.XPATH,''))不作業Python
下一篇:從懸停元素獲取定位器
