【问题标题】:Python Selenium with Chrome, how to download to a specified folder with specified file namePython Selenium with Chrome,如何下载到具有指定文件名的指定文件夹
【发布时间】:2018-04-11 16:53:07
【问题描述】:

该网站有一个下载按钮,在 python 中我可以做到

button.click()

使用网站指定的文件名将文件下载到 Chrome 下载文件夹。

有没有办法在 Windows 上更改目标文件夹和文件名?

【问题讨论】:

    标签: python windows google-chrome selenium


    【解决方案1】:

    您可以为 chrome 创建配置文件并定义测试的下载位置。这是一个例子:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = webdriver.ChromeOptions() 
    options.add_argument("download.default_directory=C:/Downloads")
    
    driver = webdriver.Chrome(chrome_options=options)
    

    【讨论】:

      【解决方案2】:

      尝试:

          download_dir = "/yourDownloadPath/"
          chrome_options = webdriver.ChromeOptions()
          preferences = {"download.default_directory": download_dir ,
                         "directory_upgrade": True,
                         "safebrowsing.enabled": True }
          chrome_options.add_experimental_option("prefs", preferences)
          driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r'/pathTo/chromedriver')
          driver.get("urlfiletodownload");
      

      【讨论】:

        【解决方案3】:

        在浪费了一些时间之后,我发现推荐的解决方案确实对我有用:

        options.add_argument("download.default_directory=C:/MyDownloadPath")
        

        这是下面对我有用的代码 sn-p。 chromedriver.exe 与我的 python 脚本在同一个文件夹中,我也想下载到同一个文件夹中。如果 chromedriver 在您的 PATH 中并且可以通过 selenium 找到,则您不需要 executable_path 参数。

        import os
        from selenium import webdriver
        
        localdir = os.path.dirname(os.path.realpath(__file__))
        chromeOptions = webdriver.ChromeOptions()
        prefs = { "download.default_directory" : localdir }
        chromeOptions.add_experimental_option("prefs", prefs)
        exe_path = os.path.join(localdir, 'chromedriver.exe')
        with webdriver.Chrome(executable_path=exe_path, options=chromeOptions) as driver:
            # do your chrome download stuff here:
            driver.get(link)
        

        我的系统:

        Windows 10
        Python 3.7.6
        Chrome 80.0.3987.132
        ChromeDriver 80.0.3987.106
        Pip Module: Selenium 3.141.0
        Date: March 2020
        

        【讨论】:

          【解决方案4】:

          有效!!!

          将目录路径存储在变量中并将变量传递给“download.default_directory”

          exepath = sys.arg[0]
          # get the path from the .py file
          Dir_path = os.path.dirname(os.path.abspath(exepath))
          # get the path of "PDF_Folder" directory
          Download_dir = Dir_path+"\\PDF_Folder\\"
          
          preferences = {"download.default_directory": Download_dir , # pass the variable
                             "download.prompt_for_download": False,
                             "directory_upgrade": True,
                             "safebrowsing.enabled": True }
          chrome_options.add_experimental_option("prefs", preferences)
          driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r'/pathTo/chromedriver')
          driver.get("urlfiletodownload");
          

          【讨论】:

            猜你喜欢
            • 2022-01-06
            • 2014-08-18
            • 2014-10-04
            • 1970-01-01
            • 2016-05-31
            • 2020-12-29
            • 2023-03-12
            • 1970-01-01
            相关资源
            最近更新 更多