且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 selenium 和 python 从弹出窗口下载并保存多个 csv 文件

更新时间:2023-08-27 19:27:34

这是您问题的答案:

您称为 find_element_by_id("submit-download-list") 的元素实际上下载了一个 PDF 文件.因此,为了未来的程序员和此问题/帖子/线程/讨论的读者的利益,您可以考虑将您的问题标题更改为使用 selenium 和 python 从弹出窗口下载和保存 PDF 文件

The element you referred as find_element_by_id("submit-download-list") actually downloads a PDF file. So for the benefit of future programmers and readers of this question/post/thread/discussion, you may consider to change your question header to Download and Save PDF file using selenium and python from popup

这是使用 selenium 和 python 从弹出窗口下载和保存 PDF 文件的代码块:

Here is the code block to Download and Save PDF file using selenium and python from popup:

import os
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

binary = FirefoxBinary('C:\Program Files\Mozilla Firefox\firefox.exe')
newpath = 'C:\home\DebanjanB'
if not os.path.exists(newpath):
    os.makedirs(newpath)

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir",newpath)
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", "")
profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
profile.set_preference("browser.download.manager.closeWhenDone", True)
profile.set_preference("pdfjs.disabled", True)

caps = DesiredCapabilities.FIREFOX
browser = webdriver.Firefox(firefox_profile=profile, capabilities=caps, firefox_binary=binary, executable_path='C:\Utility\BrowserDrivers\geckodriver.exe')
browser.maximize_window()
browser.get("https://clinicaltrials.gov/ct2/results?cond=&term=lomitapide&cntry1=&state1=&SearchAll=Search+all+studies&recrs=")
browser.find_element_by_id("save-list-link").click()
download_link = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.XPATH, "//input[@id='submit-download-list']"))
)
download_link.click()

如果这能回答您的问题,请告诉我.

Let me know if this Answers your Question.