python3 小实践(一)——selenium获取的cookie传递

from selenium import webdriver
from time import sleep
import requests
import pickle
#获取登录后的cookies
class  get_cookie1:
    
    def __init__(self, url):
        
        self.url = url
        
    def get_cookie2(self):
        
        
        driver = webdriver.Chrome()
        r = driver.get(self.url)
        driver.find_element_by_xpath("//input[@name='userName']").send_keys('admin')
        driver.find_element_by_xpath("//input[@name='password']").send_keys('123456')
        driver.find_element_by_xpath("//input[@id='btnlogin']").click()
        sleep(10)

        list_cookies= driver.get_cookies()
        
        print(list_cookies)
        cookie={}
        # 转换dict调用
        for item in list_cookies:
            cookie[item['name']]=item['value']
        print(cookie)

        cookie= driver.get_cookies()
        #print(cookie)
        
        self.cookie = cookie
        
        #print(self.cookie)
        
        return self.cookie
if __name__ =='__main__':
    c = get_cookie1("url")
    c.get_cookie2()

通过selenium提取的cookie转换一下格式就可以直接调用

python新人,代码写的有点挫,给自己获取的新知识点标个记

你可能感兴趣的:(python3 小实践(一)——selenium获取的cookie传递)