Unable to Save and Re-load Gmail Cookies with Selenium/Splinter Unable to Save and Re-load Gmail Cookies with Selenium/Splinter selenium selenium

Unable to Save and Re-load Gmail Cookies with Selenium/Splinter


You are replicating cookies on for one domain. You need to replicate for below domains as well

accounts.google.commail.google.com

And may be even more, see the screenshot below on fresh login

Domains


Example code for a browser wrapper to make set cookies easier:

class SplinterBrowserWrapper:    def __init__(self, splinter_browser):        b = self.browser = splinter_browser        b.visit('https://' + ROOT_DOMAIN)    def add_cookies(self, cookies: dict):        self.browser.cookies.add(cookies)        for cookie in self.list_cookies():            cookie['domain'] = ROOT_DOMAIN            self.add_single_cookie_dict(cookie)        self.browser.reload()    def add_single_cookie_dict(self, single_cookie_dict):        self.browser.driver.add_cookie(single_cookie_dict)    def get_cookies(self):        return self.browser.cookies.all()    def list_cookies(self):        return self.browser.driver.get_cookies()

routine:

  1. visit mail.google.com
  2. mail.google.com ask accounts.google.com: this guy login or not?
  3. accounts answer: NO!
  4. mail: OK, just another wandering guest...

guest is not happy, tries to set cookies via splinter:

  1. visit mail.google.com and set cookies
  2. cookies are set, but bound with sub-domain mail.google.com (run browser.driver.get_cookies() and check the returned list)
  3. sub-domain accounts.google.com still no cookies
  4. mail ask accounts the same question and get the same NO answer
  5. emmm...

guest knows the trick now, guest tries again:

  1. visit accounts.google.com and set cookies
  2. visit mail.google.com
  3. mail ask accounts, get answer YES
  4. mail login
  5. N~I~C~E~

another routine, another website called bilibili:

  • visit www.bilibili.com
  • www ask account.bilibili.com
  • account has no logged in cookies, account redirect browser to passport.bilibili.com
  • passport ask user for username & password
  • passport get correct auth, set cookies, and redirect back to home page as login

splinter chanllege bilibili, round 1:

  • visit www.bilibili.com, set cookies, reload page, not login
  • visit account.bilibili.com, be redirected to passport.bilibili.com
  • set cookies on passport
  • but passport not want cookies, passport NEED username & password
  • passport stay put, refuse to redirect to account or www
  • account still no cookies
  • visit account again, redirected to passport again, dead loop

splinter challenge round 2:

  • visit www.bilibili.com, set cookies, cookies are bound with www.bilibili.com
  • dig into underlying selenium driver, list all cookies with browser.driver.get_cookies(), every single cookie in this list is a dict, with keys name, value, path, domain, secure, httpOnly
  • iterate this cookies list, for every cookie dict, change value of domain to the parent domain bilibili.com, with no sub-domain
  • set these manipulated cookies back into selenium drive one by one, via browser.driver.add_cookie
  • now account see these cookies bound with its parent domain bilibili.com
  • account is obedient, account accpet these cookies, account now answer YES when anyone ask if user is login
  • reload the page and user login