#coding =utf-8
From selenium import Webdriver
From selenium.common.exceptions import nosuchelementexception
Import Unittest,time
Import requests
#登录模块函数
def login ():
U ' gdtmpd login '
Driver=webdriver. Chrome ()
Driver.get (' Login address URL ')
Nowhandle=driver.current_window_handle
Print "nowhandle:%s"%nowhandle
Driver.implicitly_wait (30)
Driver.find_element_by_link_text (U ' QQ login '). Click ()
Driver.switch_to_frame ("Ptlogin_iframe") #frame里面写frame的id值
Time.sleep (5)
driver.find_element_by_id ("Qlogin_list"). Find_element_by_css_selector ("a[uin= ' here is the QQ login number ']"). Click ()
Time.sleep (5)
usr=driver.find_element_by_id (' Username-top '). Text
Print usr
#获取与组合保存cookie
cookie = [item["name"] + "=" + item["value"] for item in Driver.get_cookies ()]
Print Cookie
cookiestr = '; '. Join (item for item in cookie)
Print Cookiestr
Url_avatar= "Https://gz.zshlife.com/api/mobile/h5.php?mod=get_my_avatar"
headers={' cookie ': cookiestr}
#访问接口在headers里面带上cookie
Response_message=requests.get (Url=url_avatar,headers=headers)
Res_url=response_message.url
Url_name=res_url.split ('/') [ -1].split ('. ') [0]
Print Url_name
if __name__ = = ' __main__ ':
Login ()
Use the Python+seleniumui Automation login to get the cookie before you go to test the interface, it's finally done today.