Skip to content
Advertisement

I can’t add cookies on python selenium webdriver

I’m trying to enter a page and then add cookies on it, but it returns an error and I don’t know why. I did it exactly like is written on the selenium docs, and here is the code i used:

local_path = rf'{os.path.dirname(os.path.realpath(__file__))}\chromedriver.exe'
driver = webdriver.Chrome(executable_path=local_path)
driver.get('https://example.com/')
driver.add_cookie({'session': 'session token'})
driver.close()

And it returns selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'name'.
I already thought that it could be the code trying to add cookies before the page is totally loaded, but it isn’t. I put a time.sleep(30) between get and add_cookie and it returned the same exception.

Advertisement

Answer

Actually it was just a wrong usage of add_cookie(). The right way to send cookies is like this:

driver.addcookie({

    'name': 'session',
    'value': 'token'

})

My error was using directly the cookie name + the value, and not with the dict template.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement