Python - 请求,Selenium - 登录时传递 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42087985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Requests, Selenium - passing cookies while logging in
提问by AntoG
I would like to integrate python Selenium and Requests modules to authenticate on a website.
我想集成 python Selenium 和 Requests 模块以在网站上进行身份验证。
I am using the following code:
我正在使用以下代码:
import requests
from selenium import webdriver
driver = webdriver.Firefox()
url = "some_url" #a redirect to a login page occurs
driver.get(url) #the login page is displayed
#making a persistent connection to authenticate
params = {'os_username':'username', 'os_password':'password'}
s = requests.Session()
resp = s.post(url, params) #I get a 200 status_code
#passing the cookies to the driver
driver.add_cookie(s.cookies.get_dict())
The problem is that when I enter the browser the login authentication is still there when I try to access the url
even though I passed the cookies generated from the requests session.
问题是,当我进入浏览器时,url
即使我传递了从请求会话生成的 cookie ,当我尝试访问时,登录身份验证仍然存在。
How can I modify the code above to get through the authentication web-page?
如何修改上面的代码以通过身份验证网页?
Can anyone help me on this issue?
Your help is much appreciated.
Best Regards.
任何人都可以帮助我解决这个问题吗?
非常感谢您的帮助。
此致。
回答by AntoG
I finally found out what the problem was.
Before making the post
request with the requests
library, I should have passed the cookies of the browser first.
The code is as follows:
我终于发现了问题所在。在post
向requests
库发出请求之前,我应该先传递浏览器的 cookie。代码如下:
import requests
from selenium import webdriver
driver = webdriver.Firefox()
url = "some_url" #a redirect to a login page occurs
driver.get(url)
#storing the cookies generated by the browser
request_cookies_browser = driver.get_cookies()
#making a persistent connection using the requests library
params = {'os_username':'username', 'os_password':'password'}
s = requests.Session()
#passing the cookies generated from the browser to the session
c = [s.cookies.set(c['name'], c['value']) for c in request_cookies_browser]
resp = s.post(url, params) #I get a 200 status_code
#passing the cookie of the response to the browser
dict_resp_cookies = resp.cookies.get_dict()
response_cookies_browser = [{'name':name, 'value':value} for name, value in dict_resp_cookies.items()]
c = [driver.add_cookie(c) for c in response_cookies_browser]
#the browser now contains the cookies generated from the authentication
driver.get(url)
回答by Charlles Pimenta
I had some issues with this code because its set double cookies to the original browser cookie (before login) then I solve this with cleaning the cookies before set the login cookie to original. I used this command:
我对这段代码有一些问题,因为它为原始浏览器 cookie 设置了双 cookie(登录前),然后我通过在将登录 cookie 设置为原始 cookie 之前清理 cookie 来解决这个问题。我使用了这个命令:
driver.delete_all_cookies()