Search code examples
pythonselenium-webdriverfirefox

Navigate Firefox with Selenium


I want to get to a particular session in firefox, so my login data is saved

from selenium import webdriver

profile_path = "/home/XXXX/.mozilla/firefox/XXXX.selenium1"  # particular profile - I hid some part of the path for privacy reason

options = webdriver.FirefoxOptions()
options.add_argument(f"--profile={profile_path}")

driver = webdriver.Firefox(options=options)
driver.get(url)

However, the get seems not to work. Firefox opens but I need to navigate to the page manually.

Instead, if I do not use a profile, i.e.,

from selenium import webdriver


driver = webdriver.Firefox()
driver.get(url)

Firefox opens the right page... but of course, I am not logged in.

I would like to get the first script to work.


Solution

  • You can try to export the session's cookies to use them in the future sessions: Start your selenium session, login to your FireFox profile, then:

    with open(path, "wb") as file:
            pickle.dump(driver.get_cookies(), file)
    

    In the next sessions load the cookies as follows:

    with open(path, "rb") as file:
            cookies = pickle.load(file)
            for cookie in cookies:
                driver.add_cookie(cookie)