如何在python中使用selenium下载文件?

k75qkfdt  于 2022-12-24  发布在  Python
关注(0)|答案(3)|浏览(268)

我想用python下载文件,但我不能这样做。我试着寻找这样做的方法,但我没有找到任何相关的资源。
下面是我的代码:

from selenium import webdriver
driver = webdriver.Chrome('/home/user/Downloads/chromedriver')

#The below link is a pdf file and not an HTML file. I want to download this file directly.

driver.get("https://authlink-files-storage.ams3.digitaloceanspaces.com/authlink/transfered_certificates_related_docs/supporting_docs_17_2020_07_24_06_25_764ffb965d1b4ae287a0d3cc01c8dd03")

现在我想下载这个文件,但我不能这样做。

q5iwbnjs

q5iwbnjs1#

如果直接下载不起作用,您可以使用打印功能解决:
1.需要使用chrome选项--kiosk-printing,一旦打开打印对话框,将自动单击打印按钮
选项= Web驱动程序.ChromeOptions()
选项.add_argument(“--自助服务终端打印”)
1.将chrome首选项定义为JSON字符串
第一个月
在上述首选项中,默认目录将用于将您的pdf保存在所需的位置。第二个首选项将自动从打印对话框中选择“另存为pdf”选项
1.添加首选项作为实验选项
选项.add_experimental_option(“首选项”,首选项)
1.使用chrome选项和首选项定义驱动程序
驱动程序= Web驱动程序.Chrome(chrome_options=选项)
1.在url中打开pdf后,您可以使用javascript打开打印对话框
驱动程序.execute_script(“窗口.打印()”)
您的pdf将以相同的标题保存在目标路径中

63lcw9qa

63lcw9qa2#

试试这个代码

from selenium import webdriver

download_dir = "C:\\Temp\\Dowmload"  # for linux/*nix, download_dir="/usr/Public"
options = webdriver.ChromeOptions()

profile = {"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}], # Disable Chrome's PDF Viewer
               "download.default_directory": download_dir , "download.extensions_to_open": "applications/pdf"}
options.add_experimental_option("prefs", profile)
driver = webdriver.Chrome('//Server/Apps/chrome_driver/chromedriver.exe', chrome_options=options)
driver.get("https://authlink-files-storage.ams3.digitaloceanspaces.com/authlink/transfered_certificates_related_docs/supporting_docs_17_2020_07_24_06_25_764ffb965d1b4ae287a0d3cc01c8dd03")
xcitsw88

xcitsw883#

你的问题的解决方法很简单,为了更好的解释,让我来帮你考虑一个场景,比如在当前框架文件夹中不点击保存为按钮就下载了一个文件,然后在验证后删除该文件。

from selenium import webdriver
 import os
 From selenium.webdriver.common.by import By
 From webdriver_manager.chrome import ChromeDriverManager

 op = webdriver.ChromeOptions()
 op.add_argument('--no-sandbox')
 op.add_argument('--verbose')
 op.add_argument("--disable-notifications")
 op.add_experimental_option("prefs", {"download.default_directory": 
 "G:/Python/Download/","download.prompt_for_download": 
 False,"download.directory_upgrade": True,"safebrowsing.enabled": True})
 op.add_argument('--disable-gpu')
 op.add_argument('--disable-software-rasterizer')
 driver = webdriver.Chrome(ChromeDriverManager().install(), 
 chrome_options=op)
 
 driver.find_element(By.XPATH, “//span[@type = ‘button’]”).click()

 def download_file_verify(self,filename):
   dir_path = "G:/Python/Download/"
    res = os.listdir(dir_path)
    try:
        name = os.path.isfile("Download/" + res[0])
        if res[0].__contains__(filename):
            print("file downloaded successfully")
    except "file is not downloaded":
        name = False
    return name

def delete_previous_file(self,filename):
    try:
        d_path = "G:/Python/Download/"
        list = os.listdir(d_path)
        for file in list:
            print("present file is: " + file)
            path = ("Download/" + file)
            if file.__contains__(filename):
                os.remove(path)
                print("Present file is deleted")
    except:
        pass

相关问题