selenium 操作 chrome driver 对 https 协议的网站进行 cookies 操作

发布于 2022-09-11 18:29:26 字数 2826 浏览 13 评论 0

环境情况

  • java 语言
  • selenium 2.53.0 版本
  • chrome 版本 72.0.3626

使用 selenium 控制 chrome driver 登录百度

通过 selenium 操作chrome浏览器登录百度,然后记录下登录之后的 cookies 信息,cookie中的secure的值都是false(这是正确的)

{"domain":".baidu.com","expiry":1812100382000,"httpOnly":true,"name":"BDUSS","path":"/","secure":false,"value":"XlrWjR3TGhIYWtaVHo41w2YY9cd"}
{"domain":".www.baidu.com","expiry":2498980384000,"httpOnly":false,"name":"sugstore","path":"/","secure":false,"value":"1"}
{"domain":".baidu.com","httpOnly":false,"name":"delPer","path":"/","secure":false,"value":"0"}
{"domain":".baidu.com","expiry":3700384001000,"httpOnly":false,"name":"BIDUPSID","path":"/","secure":false,"value":"3BA3A71DA2697"}
{"domain":".baidu.com","expiry":3700384001000,"httpOnly":false,"name":"PSTM","path":"/","secure":false,"value":"1552900379"}
{"domain":".baidu.com","httpOnly":false,"name":"H_PS_PSSID","path":"/","secure":false,"value":"1420_21086_18560_20883_28557_28608_28585_28603"}
{"domain":"www.baidu.com","expiry":1553764384000,"httpOnly":false,"name":"BD_UPN","path":"/","secure":false,"value":"12314353"}
{"domain":".baidu.com","expiry":3700384001000,"httpOnly":false,"name":"BAIDUID","path":"/","secure":false,"value":"3BA3A71DA2697:FG=1"}
{"domain":"www.baidu.com","httpOnly":false,"name":"BD_HOME","path":"/","secure":false,"value":"1"}

但是当我再次自己新打开浏览器进入百度首页,把cookies信息写进去之后,再读取cookies信息,其中的值变成了 true。导致错误 - 因为如果cookie的 secure 设置为 true之后,如果是在https网站下设置的,那么将无法进入当前域名的其他 http 的页面(丢失登录信息)。

{"domain":"baidu.com","expiry":1812100382000,"httpOnly":false,"name":"BDUSS","path":"/","secure":false,"value":"XlrWjR3TG1w2YY9cd"}
{"domain":"www.baidu.com","expiry":2498980384000,"httpOnly":false,"name":"sugstore","path":"/","secure":false,"value":"1"}
{"httpOnly":false,"name":"delPer","path":"/","secure":false,"value":"0"}
{"domain":"baidu.com","expiry":3700384001000,"httpOnly":false,"name":"BIDUPSID","path":"/","secure":false,"value":"3BA3A7CAA4"}
{"httpOnly":false,"name":"BDRCVFR[f83LjLo_kJ6]","path":"/","secure":false,"value":"mk3SLVN4HKm"}
{"domain":"baidu.com","expiry":3700384001000,"httpOnly":false,"name":"PSTM","path":"/","secure":false,"value":"1552900379"}
{"domain":"www.baidu.com","expiry":1553764384000,"httpOnly":false,"name":"BD_UPN","path":"/","secure":false,"value":"12314353"}
{"httpOnly":false,"name":"H_PS_PSSID","path":"/","secure":false,"value":"1420_21086_18560_20883_28557_28608_28585_28603"}
{"domain":"baidu.com","expiry":3700384001000,"httpOnly":false,"name":"BAIDUID","path":"/","secure":false,"value":"3BA3ACAA4:FG=1"}
{"httpOnly":false,"name":"BD_HOME","path":"/","secure":false,"value":"1"}

注意:我在Edge 和 IE 中都有测试,这两个浏览器都没有问题,唯独chrome浏览器在https的时候,写入cookies会变成 true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文