为什么我在使用Python3的Selenium时无法检索到完整的Cookie列表?

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

Why am I unable to retrieve the full list of cookies when using Selenium with Python3?

问题

I am trying to use Python3 & selenium to login to a site and extract the cookies so I can use them for a later call. However, I'm unable to retrieve the complete set of cookies.

For example, when I go to the post-authentication page manually, I can see the full list of cookie tokens from the developer tools browser GUI. These are:
csn, cpn, _ga, didomi_token, euconsent-v2, _ga_T0L2RVVYNH, _gid, _gat_gtag_UA_114622452_16, .lpac, XSRF-TOKEN, and Extranet_AL.

If I copy this complete block and use it in a subsequent request (something like)

cookies = {
    "cookies" : "_csn_: accepted, _cpn_: accepted....... etc.,"
}
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/113.0",
}
response = requests.get(api_url, headers=headers, cookies=cookies, timeout=2)

This works fine.

However, if I pull the cookies programmatically from selenium,
pickle.dump(driver.get_cookies(), open("session.pkl", "wb"))

then I only get the following tokens:
.lpac, XSRF-TOKEN, Extranet_AL, csn, and cpn

.. and they are insufficient to authenticate with the above requests.get() command.

I have double-checked that selenium is switching to the correct page and I can read the HTML elements, so I'm pretty sure it isn't a hang-up from changing tabs.

Note: I've tried Chrome and Firefox webdrivers... I'm not tied to either but both show the same problem.

Does anyone know how I can get around this? Any suggestions are really appreciated!

英文:

I am trying to use Python3 & selenium to login to a site and extract the cookies so I can use them for a later call. However, I'm unable to retrieve the complete set of cookies.

For example, when I go to the post-authentication page manually, I can see the full list of cookie tokens from the developer tools browser GUI. These are:
csn, cpn, _ga, didomi_token, euconsent-v2, _ga_T0L2RVVYNH, _gid, _gat_gtag_UA_114622452_16, .lpac, XSRF-TOKEN, and Extranet_AL.

If I copy this complete block and use it in a subsequent request (something like)

cookies = {
    "cookies" : "_csn_: accepted, _cpn_: accepted....... etc.,"
}
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/113.0",
    }
response = requests.get(api_url, headers=headers, cookies=cookies, timeout=2)

This works fine.

However, if I pull the cookies programatically from selenium,
pickle.dump(driver.get_cookies(), open("session.pkl", "wb"))

then I only get the following tokens:
.lpac, XSRF-TOKEN, Extranet_AL, csn, and cpn

.. and they are insufficient to authenticate with the above requests.get() command.

I have double checked that selenium is switching to the correct page and I can read the html elements, so I'm pretty sure it isn't a hang up from changing tabs.

Note: I've tried Chrome and Firefox webdrivers... I'm not tied to either but both show the same problem.

Does anyone know how I can get around this? Any suggestions are really appreciated!

答案1

得分: 1

很可能是基于收集到的 cookie 创建 cookie 字符串时出现了问题。请检查这一点:

cookies_json = self.driver.get_cookies()
cookies_str = ''.join(map(lambda c: '%s=%s; ' % (c['name'], c['value']), cookies_json))    
response = requests.get(url, headers={'cookie': cookies_str, 'User-Agent': userAgent})
英文:

Most probably there is an issue creating a cookie string based on the collected cookies. Check this:

cookies_json = self.driver.get_cookies()
cookies_str = ''.join(map(lambda c: '%s=%s; ' % (c['name'], c['value']), cookies_json))    
response = requests.get( url, headers={'cookie': cookies_str, 'User-Agent': userAgent})

huangapple
  • 本文由 发表于 2023年5月25日 15:28:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329833.html
匿名

发表评论

匿名网友

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

确定