0

I'm getting the cookies from selenium first after logging in and then adding the cookies to a requests session object. I get a key error and I don't understand why. Here is my code:

cookies = driver.get_cookies()
s = requests.Session()
for cookie in cookies:
    for key, value in cookie.items():
        s.cookies.set(cookie[key], cookie[value])

Cookie looks like this: {'domain': '.domain.com', 'expiry': 1666302866, 'httpOnly': False, 'name': '__utmb', 'path': '/', 'secure': False, 'value': '233684620.3.10.1666301065'} The error in question is KeyError: '.domain.com'

1 Answer 1

1
for key, value in cookie.items():
    s.cookies.set(cookie[key], cookie[value])

Since you are already iterating over the key-value pairs, cookie[value] makes no sense and is bound to fail, and cookie[key] gives back the value, not the key.

You need

for key, value in cookie.items():
    s.cookies.set(key, value)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Will mark as solution when it's possible. I can't believe I have become so blind to my own code, been sitting to long with the application in question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.