目录
使用登录可以减去每次登录的重复操作,直接操作系统登录后的菜单页面,也可以减少安全验证登录,如图像验证登录的操作。注意:cookie和token都有有效期。
cookie免登录
直接从开发者工具中获取cookie进行添加,下图为网页中多个站点的cookie,挑选需要的进行添加即可。

            
            
              python
              
              
            
          
          from selenium import webdriver
from selenium.webdriver.edge.options import Options
# 一般只需要name和value
cookie = {'name': 'ZY44', 'value': 'tLonhTkz50iHzxjhIsaaaafferr:C'}
options = Options()
# options.add_argument('--headless')
wd = webdriver.Edge(options=options)
wd.add_cookie(cookie_dict=cookie)
#for c in cookies:   # 如果是多个cookie要添加,cookies存储为列表是,使用循环添加
#   wd.add_cookie(c)
wd.refresh()  # 刷新页面
wd.get(URL)
wd.quit()raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain
(Session info: MicrosoftEdge=126.0.2592.87)
如果有上面的报错,可在wd.add_cookie(cookie_dict=cookie) 前添加一行wd.get(URL),如下:
            
            
              python
              
              
            
          
          wd.get(URL)
wd.add_cookie(cookie_dict=cookie)
wd.get(URL)通过接口获取cookie
通过接口获取cookie数据后,在selenium添加cookie使用
            
            
              python
              
              
            
          
          def get_cookies():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
    }
    host = 'https://baike.baidu.com'
    req = requests.get(host, headers=headers)
    cookie_data = req.cookies.get_dict()
    cookies = []
    for key, value in cookie_data.items():
        cookies.append(dict(name=key, value=value))
    return cookies启用浏览器绕过登录
谷歌或edge浏览器中输入:chrome://version/ 或 edge://version/ 查看配置文件夹路径,去掉后面的 \Default,然后在路径前加上 ----user-data-dir=就拼接出我们要的路径了。
            
            
              python
              
              
            
          
          profile_directory = r'--user-data-dir=C:\Users\xxx\AppData\Local\Microsoft\Edge\User Data'
            
            
              python
              
              
            
          
          # 这里使用模糊匹配,把edge开头的进程都杀掉
if platform.system() == "Windows":
    os.system("taskkill -im msedge* -f")
else:
    os.system("killall -9 msedge*")
user_data = r'C:\Users\xxx\AppData\Local\Microsoft\Edge\User Data'
profile_directory = rf'--user-data-dir={user_data}'
options = Options()
# options.add_argument('--headless')
options.add_argument(profile_directory)
wd = webdriver.Edge(options=options)
wd.maximize_window()
wd.get(URL)
wd.quit()注意:这种方法在使用时需要关闭对应浏览器的程序,否则会报错,所以在执行前需要杀掉对应浏览器的进程。上面代码使用模糊匹配查询杀掉进程,下面是全匹配。
            
            
              python
              
              
            
          
          returnCode=os.system('taskkill /F /iM chrome.exe')    # 谷歌
returnCode=os.system('taskkill /F /iM iexplore.exe')  # IE
returnCode=os.system('taskkill /F /iM firefox.exe')  # 火狐
returnCode=os.system('taskkill /F /iM msedge.exe')  # edge
assert returnCode==0 #判断浏览器进程是否杀完添加token
            
            
              python
              
              
            
          
              token = "my_token"
    options = Options()
    options.add_argument('--headless')
    wd = webdriver.Edge(options=options)
    wd.execute_script("window.localStorage.setItem('token', '%s');" % token)  # 使用selenium执行js的操作添加token
    wd.maximize_window()
    wd.get(url)
    wd.quit()