Home > Blockchain >  Why does my selenium code not save the file in the location I specified?
Why does my selenium code not save the file in the location I specified?

Time:04-25

I am working with a code that downloads a file and saves it onto a particular location. The file downloads but it keeps saving it into downloads and not into the place I specified in the code. This is the first half of the code:

#Import Packages
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options=Options()
options.add_argument("--headless")
options.add_argument("--window-size=1920,1080")

#Request File Save Location
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : 'C:\\Users\\HBee\\Desktop\\'}
chrome_options.add_experimental_option('prefs', prefs)

#Launch Website
driver=webdriver.Chrome(options=options)

params={'behavior':'allow','downloadPath':os.getcwd()}
driver.execute_cdp_cmd('Page.setDownloadBehavior',params)

driver.get("www.websitename.com")

Does anyone have any suggestions on what I'm getting wrong in my code?

CodePudding user response:

I should have specified the download path in params.

        #Import Packages
        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
        from selenium.webdriver.common.action_chains import ActionChains
        import os
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
        import time
        
        options=Options()
        options.add_argument("--headless")
        options.add_argument("--window-size=1920,1080")
        
        #Launch Website
        driver=webdriver.Chrome(options=options)
        
        #Specify File Location
        params={'behavior':'allow','downloadPath':r'C:\Users\HBee\Desktop'}
        driver.execute_cdp_cmd('Page.setDownloadBehavior',params)
        
        driver.get("websitename")
  • Related