Use the Python+seleniumui Automation login to get the cookie before you go to test the interface, it's finally done today.

Source: Internet
Author: User

#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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.