添加使用Firefox Webdriver的Cookie,但不能在PhantomJS中使用 [英] Adding Cookies working with Firefox webdriver but not in PhantomJS

查看:125
本文介绍了添加使用Firefox Webdriver的Cookie,但不能在PhantomJS中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个泡菜,里面有我通过以下命令创建的cookie

I have a pickle with cookies that I create through the following command

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

我有获取Cookie的示例代码

I have the sample code to get the cookies

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

我可以看到它很好地创建了cookie,因为如果我print很好,则add_cookie()正在做可疑的事情

I can see that it creates the cookie well because if I print it it's fine the add_cookie() is doing shady things

这给出了以下例外情况

WebDriverException:消息:{"errorMessage":无法设置 Cookie","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"219" ,"Content-Type":"application/json; charset = UTF-8",主机":"127.0.0.1:50738","User-Agent":"Python-urllib/2.7"},"httpVersion": "1.1",方法":"POST","post":"{\" sessionId \: \"391db430-154a-11e6-8a0a-ef59204729f5 \",\"cookie \":{\"domain \": \"secretWebsite \",\名称\":\"JSESSIONID \",\值\": \"8332B6099FA3BBBC82893D4C7E6E918B \",\路径\":\也是一个秘密\", \"httponly \":否,\"secure \": true}}," url:"/cookie," urlParsed:{" anchor:"," query:"," file:" cookie," directory:"/," path:"/cookie," relative:"/cookie," port:"," host:"," password:"," user:"," userInfo:" ," authority:","协议:","源:"/cookie," queryKey:{},"块:[" cookie]}," urlOriginal:"/session /391db430-154a-11e6-8a0a-ef59204729f5/cookie}} 屏幕截图:可通过屏幕查看

WebDriverException: Message: {"errorMessage":"Unable to set Cookie","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"219","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:50738","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"391db430-154a-11e6-8a0a-ef59204729f5\", \"cookie\": {\"domain\": \"secretWebsite\", \"name\": \"JSESSIONID\", \"value\": \"8332B6099FA3BBBC82893D4C7E6E918B\", \"path\": \"Also a secret\", \"httponly\": false, \"secure\": true}}","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/391db430-154a-11e6-8a0a-ef59204729f5/cookie"}} Screenshot: available via screen

要工作,我所需要做的就是将Webdriver更改为Firefox

To work, all I need is to change webdriver to Firefox

这是一个已知的PhantomJS问题吗?

Is this a known PhantomJS issue?

推荐答案

PhantomJS驱动程序似乎不支持某些键/值. 为了克服这个问题,我将使用execute_script注入最重要的变量:

It seems that some key/values are not supported by the PhantomJS driver. To overcome this issue, I would inject the most important ones with 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")

这篇关于添加使用Firefox Webdriver的Cookie,但不能在PhantomJS中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆