【发布时间】:2022-01-21 19:13:12
【问题描述】:
我正在尝试通过 python 和 selenium 以编程方式下载 csv 文件,因为我需要这样做数百次。完成这项工作的手动步骤是:
- 转至https://propertyinfo.revenue.wi.gov/WisconsinProd/search/advancedsearch.aspx?mode=advanced
- 在下拉菜单中选择县名,在文本框中输入“IOWA”,然后点击添加
- 在下拉菜单中选择单据编号,在文本框中输入“358407”,点击添加
- 点击提交
- 选择结果表的第一行(在同一选项卡中打开新页面)
- 在右侧突出显示 CSV 报告
- 点击前往保存文件。
我通过第 5 步以编程方式完成了所有工作,并且我相信第 6 步(下面的框 2...)也可以正常工作。但是,当我在代码中运行 submit2 行时,似乎没有下载任何内容。我假设对于那些比我更了解硒的人来说,这可能是一个简单的捕获/修复。我也尝试过
source = driver.find_element(By.ID, 'DTLNavigator_Report2_ReportsListBox')
action = webdriver.ActionChains(driver)
action.double_click(source)
但它似乎也不起作用。所以我要么弄乱了代码,要么似乎找不到下载的文件。您能提供的任何帮助将不胜感激。我希望我已经提供了足够的信息供您参考。
以下是我目前的代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
options = Options()
options.add_experimental_option("prefs", {"download.default_directory": r"D://Users//User//Downloads","download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": True})
options.headless = True
options.add_argument("--window-size=1920,1200")
DRIVER_PATH = "C://temp/webscraping/chromedriver.exe"
driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)
driver.get("https://propertyinfo.revenue.wi.gov/WisconsinProd/Search/Disclaimer.aspx?FromUrl=../search/advancedsearch.aspx?mode=advanced")
wait = WebDriverWait(driver,60)
driver.get("https://propertyinfo.revenue.wi.gov/WisconsinProd/search/advancedsearch.aspx?mode=advanced")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#btAgree"))).click()
box = Select(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#sCriteria"))))
box.select_by_index(4)
iE = driver.find_element(By.ID, "txtCrit")
iE.send_keys('IOWA')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#btAdd"))).click()
box = Select(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#sCriteria"))))
box.select_by_index(3)
iE = driver.find_element(By.ID, "txtCrit")
iE.send_keys('358407')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#btAdd"))).click()
submit = driver.find_element(By.ID, "btSearch").click()
myTable = driver.find_element(By.CLASS_NAME, 'SearchResults')
dataSelect = myTable.click()
box2 = Select(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#DTLNavigator_Report2_ReportsListBox"))))
box2.select_by_value('CSVMailingList')
submit2 = driver.find_element(By.ID, "ReportListButton").click()
【问题讨论】:
-
所以我能够在 options.headless = False 时让上面的代码工作,但在 options.headless = True 时不能。 csv 文件永远不会出现在下载文件夹中。任何想法为什么?
标签: python selenium csv download