Created
March 23, 2017 17:37
-
-
Save miguelmota/0f7c9d62e0c6680aeaa948558c8509fe to your computer and use it in GitHub Desktop.
Python Selenium get cookie value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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 should change the line
cookies_dict[cookie['name']] = cookie['value']
with
cookies_dict.append([cookie['name'],cookie['value']])
otherwise it doesnt work :)