I’m writing simple script for automatization of testing downloading files from web page using Python 3.9.1 and Selenium 4.20.0. I want to run Edge with ‘Profile 1’ account and default download path to my project’s folder. But since Edge opens PDF and Office files inside browser I’ve decided to add some experimental options. Project will be in future deployed to run tests in pipeline, so I’m trying to make it more portable and moved ‘User Data’ folder inside project’s folder.
But Edge ignores requirement for running with speciefic account, download path, and options for not opening PDF files.
I’ve tried recreation of ‘User Data’, ‘Profile 1’, ‘Default’ folders, changing preferences via Edge’s settings for account, hard coding full path to profile, etc. For every run Edge creates temporary user’s profile somewhere in ‘C:UsersMyUserAppDataLocalTemp33someTempUser_ID’.
Also before I’ve got error “We’re not able to open your profile to start Microsoft Edge. Try restarting” when hardcoded profile path to ‘Default’ folder of ‘User Data’ in project folder.
Below is code of setting up driver:
def setDriver():
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0"
edge_driver_path = os.path.join(os.getcwd(), 'msedgedriver.exe')
options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
options.add_experimental_option('excludeSwitches', ['disable-popup-blocking'])
options.add_argument(f"--user-data-dir = {os.path.join(os.getcwd(), 'User Data')}") #if not commented - error Opening Profile
options.add_argument('profile-directory={0}'.format(os.path.join(os.getcwd(), 'User Data\Default'))) #if not commented - error Opening Profile
#options.add_argument(f'--user-data-dir = C:\Users\MyUser\Documents\GitClone\PocSelenium\User Data\Profile 1') #if not commented - ignores options
#options.add_argument(f'--profile-directory=Profile 1') #if not commented - ignores options
options.add_argument(f'--user-agent={user_agent}')
options.add_experimental_option('prefs',
{'download.default_directory': os.path.join(os.getcwd(), 'downloadsFromDriver'),
'plugins.always_open_pdf_externally': True,
'download.prompt_for_download': False,
'download.directory_upgrade': True
})
service = webdriver.EdgeService(executable_path=edge_driver_path, service_args=['--log-level = ERROR'],
log_output=os.path.join('logsFromDriver', logFileName))
driver = webdriver.Edge(options=options, service=service)
return driver