chromedriver.exe
- 根据Chrome浏览器版本下载对应驱动文件
- 查看Chrome版本(例如:
94.0.4606.61
): 地址栏输入chrome://version/
- 下载对应版本驱动(找到
94.0.4606.61
文件夹,下载chromedriver_win32.zip
文件):http://chromedriver.storage.googleapis.com/index.html
- 将下载的驱动文件解压出
chromedriver.exe
文件,放到项目中
import os
import re
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
def login(user_name, password):
log.debug("正在模拟登录获取cookies")
service = Service("chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("http://xxx.com/login")
driver.find_element(By.ID, "userNameInput").send_keys(user_name)
driver.find_element(By.ID, "passwordInput").send_keys(password)
driver.find_element(By.ID, "submitButton").click()
sess = requests.Session()
sess.headers.clear()
for cookie in driver.get_cookies():
sess.cookies.set(cookie['name'], cookie['value'])
driver.close()
try:
log.debug("some_cookie=" + sess.cookies["some_cookie"])
return sess.cookies
except Exception:
log.debug("登录失败,请确认账号密码是否正确")
return {}