我想從此網頁下載所有州和所有合規期的 .csv報告。
換句話說,selenium 腳本會選擇一個州(例如,“DC”)一個報告期(例如,“Jan 2021 - Dec 2021”),然后單擊“提交”。然后腳本將通過單擊顯示“CSV”的影像將結果匯出到電子表格。
理想情況下,電子表格將對所有州和所有報告期執行此操作。所以最后,我的下載檔案夾會充滿電子表格。
對于我的一生,我無法弄清楚如何讓它發揮作用!
這就是我到目前為止所擁有的。沒有我認為應該有的回圈。
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 chromedriver_autoinstaller
import time
import glob
import os
chromedriver_autoinstaller.install()
chromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = "https://gats.pjm-eis.com/GATS2/PublicReports/RPSRetiredCertificatesReportingYear"
driver.get(url)
driver.find_element(By.CSS_SELECTOR, "table:nth-child(4)").click()
driver.find_element(By.ID, "SelectedState0_B-1").click()
driver.find_element(By.ID, "SelectedState0_DDD_L_LBI5T0").click()
driver.find_element(By.ID, "ReportingYear0_B-1").click()
driver.find_element(By.ID, "ReportingYear0_DDD_L_LBI0T0").click()
driver.find_element(By.CSS_SELECTOR, ".dx-vam:nth-child(2)").click()
driver.find_element(By.ID, "CSV0Img").click()
非常感謝您的幫助!我真的很感激。
uj5u.com熱心網友回復:
這是解決方案!
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
import glob
import os
chromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = "https://gats.pjm-eis.com/GATS2/PublicReports/RPSRetiredCertificatesReportingYear"
state = 'DC' # Enter State Name Here
compliance_period = 'Jan 2020 - Dec 2020' # Enter Compliance Period Here
driver.get(url)
wait.until(EC.element_to_be_clickable((By.XPATH, '(//*[@])[1]'))).click() # Clicking on Dropdown Arrow Down Icon
wait.until(EC.element_to_be_clickable((By.XPATH, '//tr[@]//td[text()="' state '"]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '(//*[@])[2]'))).click() # Clicking on Dropdown Arrow Down Icon
wait.until(EC.element_to_be_clickable((By.XPATH, '//tr[@]//td[text()="' compliance_period '"]'))).click()
driver.find_element(By.XPATH, '//*[text()="Submit"]').click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="CSV0Img"]'))).click()
print("Successfully Downloaded!")
time.sleep(10)
driver.quit()
* 根據評論中提到的情況更新了下面的另一個解決方案,我們必須讓它遍歷所有狀態和所有合規期。
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
chromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = "https://gats.pjm-eis.com/GATS2/PublicReports/RPSRetiredCertificatesReportingYear"
driver.get(url)
count_state = len(driver.find_elements(By.XPATH, '//table[@id="SelectedState0_DDD_L_LBT"]//tr'))
for i in range(1, count_state 1):
wait.until(EC.element_to_be_clickable((By.XPATH, '(//*[@])[1]'))).click() # Clicking on Dropdown Arrow Down Icon
wait.until(EC.element_to_be_clickable((By.XPATH, '(//table[@id="SelectedState0_DDD_L_LBT"]//tr)[' str(i) ']'))).click()
state_name = driver.find_element(By.XPATH, '(//table[@id="SelectedState0_DDD_L_LBT"]//tr/td)[' str(i) ']').get_attribute("textContent")
count_period = len(driver.find_elements(By.XPATH, '//table[@id="ReportingYear0_DDD_L_LBT"]//tr'))
for j in range(1, count_period 1):
wait.until(EC.element_to_be_clickable((By.XPATH, '(//*[@])[2]'))).click() # Clicking on Dropdown Arrow Down Icon
wait.until(EC.element_to_be_clickable((By.XPATH, '(//table[@id="ReportingYear0_DDD_L_LBT"]//tr)[' str(j) ']'))).click()
driver.find_element(By.XPATH, '//*[text()="Submit"]').click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="CSV0Img"]'))).click()
compliance_period_name = driver.find_element(By.XPATH, '(//table[@id="ReportingYear0_DDD_L_LBT"]//tr/td)[' str(j) ']').get_attribute("textContent")
print("Successfully Downloaded for State:", state_name, " and Compliance Period: ", str(compliance_period_name))
print("\n")
time.sleep(10)
driver.quit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/488595.html
