Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python Selenium get cookie value
import time
from selenium import webdriver
driver = webdriver.Chrome('./chromedriver')
driver.get('https://ui.lkqd.com/login')
assert 'LKQD' in driver.title
time.sleep(2)
username_field = driver.find_element_by_name('username')
username_field.send_keys('myusername')
password_field = driver.find_element_by_name('password')
password_field.send_keys('mypassword')
submit_button = driver.find_elements_by_xpath("//*[contains(text(), 'Sign In')]")[0]
submit_button.click()
time.sleep(2)
cookies_list = driver.get_cookies()
cookies_dict = {}
for cookie in cookies_list:
cookies_dict[cookie['name']] = cookie['value']
session_id = cookies_dict.get('session')
print(session_id)
driver.quit()
@napster2091

This comment has been minimized.

Copy link

commented Feb 24, 2018

you should change the line
cookies_dict[cookie['name']] = cookie['value']
with
cookies_dict.append([cookie['name'],cookie['value']])
otherwise it doesnt work :)

@prashanth-sams

This comment has been minimized.

Copy link

commented May 9, 2018

and cookies_dict = {}
should be
cookies_dict = []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.