举例:
def test_003(): url = 'https://xx.com/user-login.html' # 启动浏览器 browser = webdriver.Chrome('/usr/local/bin/chromedriver') browser.get(url=url) browser.maximize_window() # 设置页面加载最大时间 browser.set_page_load_timeout(80) # 设置隐式等待时间 browser.implicitly_wait(80) # 打开网页,获取 Cookie time.sleep(3) browser.find_element(By.XPATH,'//input[@id="account"]').send_keys("sdd") browser.find_element(By.XPATH,'//input[@name="password"]').send_keys("sddsd") browser.find_element(By.XPATH,'//button[@id="submit"]').click() time.sleep(3) print("以下为cookie") # 获取所有?cookie的信息 cookies = browser.get_cookies() print(browser.get_cookies()) cookie = [item["name"] + "=" + item["value"] for item in cookies] cookiestr = ';'.join(item for item in cookie) # headers_cookie = { # "cookie": cookiestr # 通过接口请求时需要cookies等信息 # } # print(headers_cookie) url2 = 'https://pms.uniontech.com/caselib-createCase-237-0.html' # response = requests.get(url=url2, headers=headers_cookie) # print(response.request.body) headers = { 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryiC26IDbJQJp8BU18', "cookie": cookiestr } multipart_encoder = MultipartEncoder( fields={ "lib": "237", "module": "0", "type": "feature", "scenes": "default", "stage[]": "", "baseline": "", "important": "", "execution": "", "isAutomation": "否", "title": "test456", "color": "", "pri": "3", "precondition": "", "steps[1]": "1", "stepType[1]": "item", "expects[1]": "2", "steps[2]": "", "stepType[2]": "item", "expects[2]": "", "steps[3]": "", "stepType[3]": "item", "expects[3]": "", "keywords": "", "labels[]": "", "files[]": "" } ) headers['Content-Type'] = multipart_encoder.content_type res = requests.post(url2, headers=headers, data=multipart_encoder, verify=False) print(res.status_code)
标签:web,cookies,item,表单,headers,cookie,print,browser From: https://www.cnblogs.com/xxx1234-11225/p/17679384.html