Search code examples
pythonselenium-webdrivercookiesseleniumbase

Problem with Saving and Loading cookies with SeleniumBase


I'm trying to save the authentication of my account on Discord and the file successfully saved the cookies after I logged in but when I tried to load the cookies It didn't change the cookies at all and It asked me to log in again this problem happened with raw selenium as well don't provide a solution with pickle package I tried everything only worked with playwright package and I hope I find solution with seleniumbase

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class MyTestClass(BaseCase):
    def test_swag_labs(self):
        self.open("https://discord.com/")
        self.sleep(5)
        # self.save_cookies(name="cookies.txt")
        self.load_cookies(name="cookies.txt")
        self.open("https://discord.com/channels/@me")
        self.sleep(5)


Solution

  • Saving and loading cookies might not be enough to recreate the state. A better option would be setting a custom user_data_dir for the tests, and then reusing that user_data_dir for later tests.

    With the SeleniumBase pytest format, you can set it via command-line option:

    pytest --user-data-dir=my_data_dir