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
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:
- visit
mail.google.com
mail.google.com
askaccounts.google.com
: this guy login or not?accounts
answer: NO!mail
: OK, just another wandering guest...
guest is not happy, tries to set cookies via splinter:
- visit
mail.google.com
and set cookies - cookies are set, but bound with sub-domain
mail.google.com
(runbrowser.driver.get_cookies()
and check the returned list) - sub-domain
accounts.google.com
still no cookies mail
askaccounts
the same question and get the same NO answer- emmm...
guest knows the trick now, guest tries again:
- visit
accounts.google.com
and set cookies - visit
mail.google.com
mail
askaccounts
, get answer YESmail
login- N~I~C~E~
another routine, another website called bilibili:
- visit
www.bilibili.com
www
askaccount.bilibili.com
account
has no logged in cookies,account
redirect browser topassport.bilibili.com
passport
ask user for username & passwordpassport
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 topassport.bilibili.com
- set cookies on
passport
- but
passport
not want cookies,passport
NEED username & password passport
stay put, refuse to redirect toaccount
orwww
account
still no cookies- visit
account
again, redirected topassport
again, dead loop
splinter challenge round 2:
- visit
www.bilibili.com
, set cookies, cookies are bound withwww.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 keysname
,value
,path
,domain
,secure
,httpOnly
- iterate this cookies list, for every cookie dict, change value of
domain
to the parent domainbilibili.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 domainbilibili.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