Adding cookies that work with the Firefox web browser but not in PhantomJS

I have a cookie puppet that I create with the following command

def doLogin(driver):
    #do login stuff
    pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))

I have sample code for receiving cookies

driver = webdriver.PhantomJS()
self.doLogin(driver)
driver.delete_all_cookies()
for cookie in pickle.load(open("cookies.pkl", "rb")):
    driver.add_cookie(cookie)

I see that it creates a cookie well, because if I am print, it’s great that add_cookie () does shadow things

This gives the following exception

WebDriverException: : { "errorMessage": " Cookie", "": { "": { "Accept": "/JSON", "Accept-Encoding": "", "": "", "Content-Length": "219", "Content-Type": "/JSON; = UTF-8", "": "127.0.0.1:50738", "User-Agent": "Python-urllib/2.7" }, "httpVersion": "1.1", "": "POST", "": "{\" SESSIONID\ ": \" 391db430-154a-11e6-8a0a-ef59204729f5 \ ",\" cookie \ ": {\" domain \ ": \" secretWebsite \ ",\" name \ ":\" JSESSIONID \ ",\" value \ ": \" 8332B6099FA3BBBC82893D4C7E6E918B \ ",\" \ ":\" \ ", \" httponly \ ": false,\" secure \ ": }}" "URL": "/", "urlParsed": { "": "", "": "," ":" "," ":" / "," ":" / "" ": "/", "": "," ": "," ": "," ": "," USERINFO ":" "" ": "," ": "," ": "/", "queryKey ": {}," ": [" "]}," urlOriginal ":" //391db430-154a-11e6-8a0a-ef59204729f5/ "}} :

, webdriver Firefox

PhantomJS?

+4
1

, / PhantomJS. , execute_script:

def save_cookies(driver, file_path):
    LINE = "document.cookie = '{name}={value}; path={path}; domain={domain}; expires={expires}';\n"
    with open(file_path, 'w') as file :
        for cookie in driver.get_cookies() :
            file.write(LINE.format(**cookie))

def load_cookies(driver, file_path):
    with open(file_path, 'r') as file:
        driver.execute_script(file.read())


from selenium import webdriver

driver = webdriver.PhantomJS()

# load the domain
driver.get("https://stackoverflow.com/users/login")

# save the cookies to a file
save_cookies(driver, r"cookies.js")

# delete all the cookies
driver.delete_all_cookies()

# load the cookies from the file
load_cookies(driver, r"cookies.js")
+6

All Articles