0

I am trying to open the default profile using selenium in python using the following code :

from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.keys import Keys

options = EdgeOptions()
options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
options.use_chromium = True
options.add_argument("user-data-dir=C:\\Users\\UserName\\AppData\\Local\\Microsoft\\Edge\\User Data")
options.add_argument("profile-directory=Default")
options.add_argument("--start-maximized")
options.add_argument("--remote-debugging-port=9222")
driver = Edge(options = options,executable_path=r'C:\Users\UserName\Desktop\Programs\msedgedriver.exe')

driver.get ("https://www.google.com")
driver.close()

When I run the code, the default profile browser opens successfully but the driver closes automatically due to some error. The line to get www.google.com did not run.

The error message:

Warning (from warnings module):
  File "C:\Users\UserName\Desktop\MS Credit automation\Test.py", line 12
    driver = Edge(options = options,executable_path=r'C:\Users\UserName\Desktop\Programs\msedgedriver.exe')
DeprecationWarning: Selenium Tools for Microsoft Edge is deprecated. Please upgrade to Selenium 4 which has built-in support for Microsoft Edge (Chromium): https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/#upgrading-from-selenium-3
Traceback (most recent call last):
  File "C:\Users\UserName\Desktop\Programs\Test.py", line 12, in <module>
    driver = Edge(options = options,executable_path=r'C:\Users\UserName\Desktop\Programs\msedgedriver.exe')
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\msedge\selenium_tools\webdriver.py", line 107, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Microsoft Edge failed to start: exited normally.
  (chrome not reachable)
  (The process started from msedge location C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe is no longer running, so msedgedriver is assuming that msedge has crashed.)

I am using

  • python 3.11.1
  • edge 115.0.1901.188
  • edge driver 115.0.1901.188
  • windows 11

When I remove the code to direct the path to open the default profile, the code runs fine. But a new profile is created each time. The lines at question are :

options.add_argument("user-data-dir=C:\\Users\\UserName\\AppData\\Local\\Microsoft\\Edge\\User Data")
options.add_argument("profile-directory=Default")

I would like to know the problem or an alternative solution to the problem please.

JBK2
  • 3
  • 4

2 Answers2

0

Refer the below working code to launch browser using default profile in python selenium:

import time

from selenium.webdriver.edge.options import Options
from selenium import webdriver

options = Options()
options.add_argument("user-data-dir=C:\\Users\\<username>\\AppData\\Local\\Microsoft\\Edge\\User Data1")
options.add_argument("--start-maximized")
driver = webdriver.Edge(options = options)
driver.get ("https://www.google.com")
time.sleep(30)

Note: You need to use selenium version 4.6.0 or above. I used latest version 4.11.0 while testing the above code.

Explanation: Below line will create a copy of User Data folder as User Data1

options.add_argument("user-data-dir=C:\\Users\\<username>\\AppData\\Local\\Microsoft\\Edge\\User Data1")

Copied folder:

enter image description here

And selenium uses the Default profile from User Data1 folder to launch browser. See below:

enter image description here

Shawn
  • 4,064
  • 2
  • 11
  • 23
  • As mentioned I have upgraded to selenium 4 and the code you gave did work fine. But it created a new Profile instead of opening the default profile. In my case the default profile is in the path "C:\Users\UserName\AppData\Local\Microsoft\Edge\User Data\Default". As additional note, I tried the previous method in selenium 4 but no luck here. – JBK2 Aug 02 '23 at 13:20
  • Yes the solution in my answer is to create a replica copy of Default profile and use it. Since it's a replica of Default profile, it will be as good as Default profile. – Shawn Aug 02 '23 at 13:24
  • I need the user data, the login status. Which is not being replicated to the new profile. – JBK2 Aug 02 '23 at 13:34
  • User data should be there in the copied profile. – Shawn Aug 02 '23 at 13:35
  • The data was not copied, it was asking for login – JBK2 Aug 02 '23 at 14:06
0

It looks like you have already opened the "Default" profile, so the selenium project does not work when you try to open the "Default" profile again. You have to make sure there's only one "Default" profile session at the same time.

You can even use the following command line to kill all the remaining "Default" profile processes:

taskkill /f /im msedge.exe
Kendrick Li
  • 1,355
  • 1
  • 2
  • 10
  • Even with no Edge browser in the task manger list, I get the same problem. And this does not just happen for default profile but for every other profile. – JBK2 Aug 03 '23 at 15:09
  • @JBK2 AFAIK, this error is usually shown when the specified profile has already been opened. Would you please create a new profile and test again with that new profile? – Kendrick Li Aug 07 '23 at 10:00