Selenium Webdriver in Python - files download directory change in Chrome preferences

前端 未结 6 1592
逝去的感伤
逝去的感伤 2020-11-27 05:09

I\'m using Selenium Webdriver (in Python) to automate the downloading of thousands of files. I want to set Chrome\'s download folder programmatically. After reading this, I

6条回答
  •  时光说笑
    2020-11-27 05:39

    Pass the variable to "download.default_directory"

    Store the dir path in variable and pass the variable to "download.default_directory"

    Note: Both .py file and Folder "PDF_Folder" in same location and file should download in Folder "PDF_Folder"

    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");
    

提交回复
热议问题