pythonpython-3.xselenium-webdrivercookiesmicrosoft-edge

Python & Selenium 4 & Edge Browser | Load personal browser profile (including cookies)


Using Selenium 4, I am trying to load up my personal browser profile (including cookies), so that it can load into the websites I have previously logged in to. I am using the edge browser. When testing my code snippet, it does not seem to load my browser profile, instead creating a new one (profile 1). I've ensured that the path to the profile is the correct one.

My code snippet:

    edge_options = webdriver.EdgeOptions()
    edge_options.use_chromium = True
    edge_options.add_argument("no-sandbox")
    edge_options.add_argument("disable-dev-shm-usage")
    edge_options.add_argument("disable-features=LockProfileCookieDatabase")
    edge_options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Microsoft\\Edge\\User Data\\Default")

    driver = webdriver.Edge(options=edge_options)
    targeturl = 'https://www.targeturl.com/'
    driver.get(targeturl)

I was expecting it to load my browser profile and then visit the target url, which should be behind a login, to confirm that it does load the cookies too.

Is there something I am missing?


Solution

  • As Denel mentioned in the comment, the answer was to declare the profile name separate from its path like this:

    edge_options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Microsoft\\Edge\\User Data")`
    edge_options.add_argument("profile-directoy=Default")
    

    instead of this:

    edge_options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Microsoft\\Edge\\User Data\\Default")