I'm still learning how to use Selenium Python and am having an issue changing the default download directory. I'm hoping a fresh or more experienced set of eyes can help.
I'm rocking with Python 3.10.11 / Selenium 4.10 / Chrome (whatever latest version is)
Below is the relevant code -- the entire script runs with no errors, but instead of using the directory path I supply it'll just create a subfolder called "downloaded_files" from whatever directory the script is running from and dump the files there.
from selenium import webdriver
from seleniumbase import Driver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
download_path = "\\\E275proddwdb\kyote"
# Set download folder
op = webdriver.ChromeOptions()
config = {"download.default_directory":download_path, "safebrowsing.enabled":"false"}
op.add_experimental_option("prefs", config)
op.headless = False
##Call Chrome Browser
service = Service()
options = webdriver.ChromeOptions()
browser = Driver(browser="chrome", headless=False)
browser.get("https://www.kyote.org/mc/login.aspx?url=kplacementMgmt.aspx")
Any help in figuring out how I messed this up would be greatly, greatly appreciated!
It looks like you're mixing two separate frameworks into one. You have webdriver
with those options
, and you have seleniumbase
, which is configured differently. The downloaded_files
folder is hard-coded in constants
(seleniumbase/fixtures/constants.py), and for good reason. That folder is uniquely-named so that files downloaded to that folder don't get mixed up with anything else outside seleniumbase
.
Here's a working script for the latest version of seleniumbase
(4.19.0
):
from seleniumbase import Driver
driver = Driver(browser="chrome", headless=False)
try:
driver.get("https://www.kyote.org/mc/login.aspx?url=kplacementMgmt.aspx")
driver.type('input[id*="UserName"]', "MyName")
driver.type('input[id*="Password"]', "PASSWORD")
driver.sleep(2)
driver.click('[value="Log In"]')
driver.sleep(4)
finally:
driver.quit()
The new direct driver
methods such as click()
and type()
were added only this week (in 4.18.9
), so you'll definitely want to make sure you have the latest version or the one immediately preceding that.