-1

I am trying the following lines

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

opt = Options()

opt.headless = True
opt.add_experimental_option ("debuggerAddress", "localhost:9090")
opt.add_argument("download.default_directory=C:/Users/Future/Desktop")
driver = webdriver.Chrome(executable_path="D:/Webdrivers/chromedriver.exe", options=opt)
driver.switch_to.window(driver.window_handles[0])

The file is downloaded but in the Downloads folder although I tried in the code to change the path of the default folder.

The code is designed to run on an already open browser. Any ideas?

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • 2
    Does this answer your question? [Downloading a file at a specified location through python and selenium using Chrome driver](https://stackoverflow.com/questions/35331854/downloading-a-file-at-a-specified-location-through-python-and-selenium-using-chr) – Shradha Jun 24 '21 at 11:16
  • I already tried the solutions on the link but the file is downloaded in the Downloads folder. – YasserKhalil Jun 24 '21 at 11:30

1 Answers1

1

I believe you are looking for .add_experimental_option(...

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_experimental_option("prefs", {
  "download.default_directory": r"C:/Users/Future/Desktop",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing_for_trusted_sources_enabled": False,
  "safebrowsing.enabled": False
})

driver = webdriver.Chrome(executable_path=r'D:/Webdrivers/chromedriver.exe', options=opt)
url = 'https://stackoverflow.com/questions/68114651/download-file-to-default-location-selenium-python/68115019#68115019'

# Load URL
driver.get(url)
Jortega
  • 3,616
  • 1
  • 18
  • 21
  • Thank you very much. i tried the code and I got `InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions from invalid argument: unrecognized chrome option: prefs` – YasserKhalil Jun 24 '21 at 11:52
  • @YasserKhalil I updated the answer to open the webpage of this question with the options loaded. If this does not work for you, what version of Python and Chrome are you running? – Jortega Jun 24 '21 at 12:16
  • Thanks a lot. In fact, I am working on an open browser using this approach: using cmd I typed `cd C:\Program Files\Google\Chrome\Application` then `chrome.exe --remote-debugging-port=9090 --user-data-dir="D:\Webdrivers\MyProfile"` then navigating to the url which requires credentials manually then, at last, I ran the python code to begin the download process. Hope this information helps. – YasserKhalil Jun 24 '21 at 13:21
  • @YasserKhalil When you navigate to the url are you going to `http://localhost:9090`? This is the part I am getting stuck. – Jortega Jun 24 '21 at 19:52
  • This is as a port for the selenium code to work on so as to work on that instance of chrome. I am using that instance to navigate to my website and doing some steps manually then finally to run the code. – YasserKhalil Jun 25 '21 at 03:07