selenium获取cookie

 参考地址:https://www.cnblogs.com/lingwang3/p/7750156.html

 

# 获取cookie
import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.delete_all_cookies()
login_url = 'https://sso.accounts.dowjones.com/login?client=5hssEAdMy0mJTICnJNvC9TXEw3Va7jfO&protocol=oauth2&redirect_uri=https%3A%2F%2Faccounts.wsj.com%2Fauth%2Fsso%2Flogin&scope=openid%20idp_id%20roles%20email%20given_name%20family_name%20djid%20djUsername%20djStatus%20trackid&response_type=code&nonce=02a76f8f-7b75-4095-82c4-ec8af2ae7e1a&connection=DJldap&ui_locales=en-us-x-wsj-19-2&mg=prod%2Faccounts-wsj&state=0k67vXdeRlXvGO2UEftM5cB0f-4Nt458#!/signin'
driver.get(login_url)
user = driver.find_element_by_css_selector('input[name="username"]')
user.send_keys('zcw@live.com')
pwd = driver.find_element_by_css_selector('input[name="password"]')
time.sleep(2)
pwd.send_keys('wise123')
time.sleep(3)
driver.find_element_by_css_selector('button[class="solid-button basic-login-submit"]').click()
time.sleep(5)
driver.switch_to_default_content()
time.sleep(5)
list_cookies = driver.get_cookies()
for t in list_cookies:
    if t['name'] == 'djcs_session':
        djcs_session = t['value']
        break
    else:
        djcs_session = False

if djcs_session:
    header = {
        "cookie": "djcs_session=%s" % djcs_session
    }
else:
    exit("cookie get faild..")
print(header)

 

posted on 2018-02-01 16:00  孔扎根  阅读(603)  评论(1编辑  收藏  举报

导航