demo-1
#-*-coding-*-## 标题
from selenium import webdriver
import time
#实例化一个浏览器
driver = webdriver.Chrome()
#设置窗口大小
#driver.set_window_size(1920, 1080)
#最大化窗口
webdriver.maximize_window()
#发送请求
driver.get("https://www.baidu.com/")
#元素定位的方法
input = driver.find_element_by_id('kw').send_keys("沙雕") # 获取输入框
searchButton = driver.find_element_by_id("su").click() # 获取搜索按钮
#运行页面截屏保存
driver.save_screenshot("./baidu.png")
#print(driver.page_souce) # 浏览器中 elements 的内容
print(driver.current_url)
#driver 获取 cookie
#cookies = driver.get_cookies()
cookies = driver.get_cookie("Cookie")
print(cookies)
#cookies = {i["name"] : i["value"] for i in cookies}
print("cookies %s " % cookies)
#退出浏览器
time.sleep(3)
driver.quit()
demo -2
import requests
requests.packages.urllib3.disable_warnings()
import json
uid = "xx"
password = "yyyy"
login_url = "https://www.xx.com/login/login.do?redirect=https%3A%2F........"
driver = webdriver.Chrome()
#方法一:通过 driver.find_element_by_xpath(' /html/body') 获取 token 的HTML 页面,再拿到 data 中 的 token 值
driver.find_element_by_id('uid').send_keys("沙雕") # 获取 输入框,并输入 name
driver.find_element_by_id('password').send_keys("沙雕") # 获取 输入框,并输入 password
searchButton = driver.find_element_by_calss_name("btn") # 获取搜索按钮
searchButton.click() # 点击登录按钮
redirect_login_url = "https://www.xx.com/auth/api/v1/token" # 获取 token api
driver.get(redirect_login_url )
body = driver.find_element_by_xpath(' /html/body')
body_json = json.loads(body.text)
#获取 token
auth_info = body_json["data"]
print("auth_info : %s" % auth_info )
time.sleep(3)
driver.quit()
"""
demo-3 方法二: requests 请求:
login_info = requests.post(
login_url,
data={
"uid":uid,
"password":password,
"verifyCode":"2345",
"actionFlag":"loginAuthenticate"
}
)
cookies = requests.utils.dict_from_cookiejar(login_info, cookies)
print(cookies)
info = requests.get("https://www.xx.com/auth/api/v1/token")
if info.status_code == 200:
auth_info = info.json().get("data")
print("auth_info : %s" % auth_info )
time.sleep(3)
driver.quit()
"""