如何读取 HttpOnly cookie 的值?

huangapple go评论88阅读模式
英文:

How can I read the value of a HttpOnly cookie?

问题

我正在使用Python和Selenium浏览一个网站,一旦登录,我想要将会话(导出Cookie)从Selenium浏览器传输到Python的requests会话,但我注意到有一个名为"session-token"的Cookie未被传输,我还注意到这个Cookie被标记为:

  • 仅主机
  • 安全
  • HttpOnly

我已经阅读了这个问题,使用Postman的拦截器桥扩展并同步Cookie,我注意到"session-token" Cookie被导出到桌面程序,这是如何可能的?有没有办法读取这个Cookie的值?

我尝试直接使用requests登录,但Cookie没有被设置。
我还尝试使用控制台打印Cookie,这是代码:

var theCookies = document.cookie.split(';');
for (var i = 0 ; i <= theCookies.length; i++) {
   console.log(theCookies[0] );
}

但Cookie没有被打印出来。

英文:

I'm using python and selenium to browse a website, once logged in I would like to transfer the session (exporting cookies) from the selenium browser to a python requests session, but I noticed that there is a cookie called "session-token " which is not transferred, I also noticed that this cookie is marked as:

  • HostOnly
  • Secure
  • HttpOnly

I've already read this question, using Postman's Interceptor bridge extension and synchronizing cookies, I noticed that the "session-token" cookie is exported to the desktop program, how is this possible? Is there any way to read the value of this cookie?

I tried logging in directly using requests but the cookie is not being set.
I also tried to print the cookies using the console, this is the code:

var theCookies = document.cookie.split(&#39;;&#39;);
for (var i = 0 ; i &lt;= theCookies.length; i++) {
   console.log(theCookies[0] );
}

but the cookie is not printed.

答案1

得分: 1

"session-token" cookie 是一个安全的 HttpOnly cookie。你可以查看这个链接:https://stackoverflow.com/questions/72889905/how-can-i-read-httponly-and-secure-cookies-in-the-browser-with-javascript。

如果需要的话,你可以手动提取 cookies:

driver = webdriver.Chrome()
# 执行必要的操作以登录并导航到所需的页面
# ...

# 从 Selenium 获取 cookies
cookies = driver.get_cookies()
英文:

The "session-token" cookie you mentioned is likely being set as a secure and HttpOnly cookie.

see this link:
https://stackoverflow.com/questions/72889905/how-can-i-read-httponly-and-secure-cookies-in-the-browser-with-javascript.

if you want you can extract cookies manually:

driver = webdriver.Chrome()
# Do necessary actions to log in and navigate to the desired page
# ...

# Get cookies from Selenium
cookies = driver.get_cookies()

答案2

得分: 0

如果您正在使用selenium,有一种方法可以获取所有的cookie:

driver = webdriver.Chrome()

# 这将返回所有的cookie内容
cookies = driver.get_cookies()

如果您倾向于使用Python的requests,您可以使用以下方法实现:

session = requests.Session()
resp = session.get("your-target-url")

# 这将以字典对象的形式返回
print(resp.cookies.get_dict())
英文:

If you are using selenium there's a method to get all of the cookies

driver = webdriver.Chrome()

# this will return all the cookies context
cookies = driver.get_cookies()

if you tend to using Python's requests, you can achieve that with called this method

session = requests.Session()
resp = session.get(&quot;your-target-url&quot;)

# this will return as dictionary object
print(resp.cookies.get_dict())

huangapple
  • 本文由 发表于 2023年6月1日 02:43:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定