F
F
fandorin_official2019-08-21 23:00:48
Python
fandorin_official, 2019-08-21 23:00:48

How to save and then load cookies in PhantomJS?

Until this beautiful evening, I saved cookies in this way:

with open("cookie/1.pkl", "wb") as cookiefile:
        pickle.dump(browser.get_cookies(), cookiefile)

and then loaded like this:
with open("cookie/1.pkl, "rb") as cookiefile:
            cookies = pickle.load(cookiefile)
            for cookie in cookies:
                browser.add_cookie(cookie)

Today I tried to crawl to PhantonJS and ran into the problem of saving and loading cookies.
Gives an error messageselenium.common.exceptions.InvalidCookieDomainException: Message: {"errorMessage":"Can only set Cookies for the current domain","request":{"headers":{"Accept":"application/json","Accept-Encoding ":"identity","Content-Length":"169","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:54294","User-Agent ":"selenium/3.141.0 (python mac)"},"httpVersion":"1.1","method":"POST","post":"{\"cookie\": {\"domain\": \"ex.ru\", \"httponly\": false, \"name\": \"viewport\", \"path\": \"/\", \"secure\": false, \" value\": \"768\"},\"sessionId\": \"3e5100e0-c44e-11e9-b7ad-81c50dc259c4\"}","url":"/cookie","urlParsed":{"anchor":"","query":"", "file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password ":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{}," chunks":["cookie"]},"urlOriginal":"/session/3e5100e0-c44e-11e9-b7ad-81c50dc259c4/cookie"}}"url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/ cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority" :"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/3e5100e0-c44e- 11e9-b7ad-81c50dc259c4/cookie"}}"url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/ cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority" :"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/3e5100e0-c44e- 11e9-b7ad-81c50dc259c4/cookie"}}directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user": "","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]} ,"urlOriginal":"/session/3e5100e0-c44e-11e9-b7ad-81c50dc259c4/cookie"}}directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user": "","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]} ,"urlOriginal":"/session/3e5100e0-c44e-11e9-b7ad-81c50dc259c4/cookie"}}"source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/3e5100e0-c44e-11e9-b7ad-81c50dc259c4/cookie"}}"source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/3e5100e0-c44e-11e9-b7ad-81c50dc259c4/cookie"}}
Screenshot: available via screen

Who can tell me how to work with cookies on PhantomJS?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question