Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created March 23, 2017 17:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save miguelmota/0f7c9d62e0c6680aeaa948558c8509fe to your computer and use it in GitHub Desktop.
Save miguelmota/0f7c9d62e0c6680aeaa948558c8509fe to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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