1. 程式人生 > >利用python+seleniumUI自動化登錄獲取cookie後再去測試接口,今天終於搞定了

利用python+seleniumUI自動化登錄獲取cookie後再去測試接口,今天終於搞定了

zsh use avatar get 獲取 mod .text 獲取cookie mob

#coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest,time
import requests


#登錄模塊函數
def login():
u‘‘‘gdtmpd登錄‘‘‘
driver=webdriver.Chrome()
driver.get(‘登錄地址url‘)
nowhandle=driver.current_window_handle
print "nowhandle:%s"%nowhandle
driver.implicitly_wait(30)
driver.find_element_by_link_text(u‘QQ登錄‘).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=‘這裏是QQ登錄號碼‘]").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()

利用python+seleniumUI自動化登錄獲取cookie後再去測試接口,今天終於搞定了