1

I'm busy creating an automation tool for a certain website but can't seem to find an option to store cookies between two different runs. The main problem is that the website consists of www.website.com/beforelogin and www.website.com/afterlogin, every other url is just being redirected to one of these depending on whether you're logged in or not. Since there is no 'overlapping url'/'dummy url' where I can save cookies the first time I log in manually and load them every other time from then on, I don't really see how to fix this. This is my code for saving and loading the cookies.

    def save_cookies(self):
        self.browser.get("http://www.website.com") # will be redirected to www.website.com/afterlogin at where it will save the cookies
        pickle.dump(self.browser.get_cookies(), open("cookies.pkl", "wb"))

    def load_cookies(self):
        self.browser.get("http://www.website.com") # will be redirected to www.website.com/beforelogin at where it will load the cookies
        if not os.path.isfile("cookies.pkl"):
            return False

        cookies = pickle.load(open("cookies.pkl", "rb"))
        print("loading cookies")
        for cookie in cookies:
            print("Adding cookies")
            self.browser.add_cookie(cookie)

        self.refresh()

The solution of finding a common dummy url as talked about in How to set a cookie to a specific domain in selenium webdriver with python? is, as explained above, not really a solution.

Solutions like this one selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain while executing tests in Django with Selenium isn't an option either since there is no same url for the before and after login where cookies are saved and loaded.

questioning
  • 245
  • 1
  • 4
  • 18

0 Answers0