I would like to write a script (on this webpage) to select the first county, click Generate Excel
(the file will download), select the second county, click Generate Excel
, and so forth for all the counties.
What I have so far:
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get("https://www2.calrecycle.ca.gov/LGCentral/DisposalReporting/Origin/ExportByCounty")
driver.set_window_size(1920, 1048)
dropdown = driver.find_element(By.ID, "CountyID")
dropdown.find_element(By.XPATH, "//option[. = '[Alameda]']").click()
element = driver.find_element(By.ID, "CountyID")
actions = ActionChains(driver)
actions.move_to_element(element).click_and_hold().perform()
element = driver.find_element(By.ID, "CountyID")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
element = driver.find_element(By.ID, "CountyID")
actions = ActionChains(driver)
actions.move_to_element(element).release().perform()
driver.find_element(By.ID, "SearchButton").click()
The issue is that I need to change "//option[. = '[Alameda]']"
to each county which would be time-consuming. Is there a way I can cycle through a list of the counties? Or I can select all the available counties?
THANK YOU for your time and help with this. I value any feedback/ideas/comments.
CodePudding user response:
You can capture the option
tag inside the select
element and loop through it.
This should work for you (tested on Windows 10; Chrome version 100):
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path='D://chromedriver/100/chromedriver.exe')
wait = WebDriverWait(driver, 20)
url = "https://www2.calrecycle.ca.gov/LGCentral/DisposalReporting/Origin/ExportByCounty"
driver.get(url)
select_el = wait.until(EC.presence_of_element_located((By.XPATH, '//select[@id="CountyID"]')))
counties = select_el.find_elements(By.TAG_NAME, 'option')
total_counties = len(counties)
# len - 1 because the 1st option is blank
print(f"Total counties: {total_counties - 1}")
btn = driver.find_element(By.XPATH, '//button[@id="SearchButton"]')
for i in range(1, len(counties)):
print(f"Clicking County - {counties[i].text}")
counties[i].click()
btn.click()
time.sleep(2)