Selenium实现登录验证

一、Selenium登录验证

1.1、隐藏“正在受到自动软件的控制”字符

当我们在使用webdriver时,浏览器上方总会出现 “正在受到自动软件控制” 字样

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
# 隐藏 正在受到自动软件的控制 这几个字
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

1.2、禁用 Blink 渲染引擎

"--disable-blink-features" : 命令行标志,传递给 Chrome 浏览器以禁用 Blink 渲染引擎的某些功能。

"Blink": 渲染引擎,被 Chrome 和其他基于 Chromium 的浏览器用来显示网页内容。

"--disable-blink-features" 标志接受一个由逗号分隔的要禁用的功能列表。可以用此标志禁用的一些功能包括(常用):

(1)"AutomationControlled":禁用仅在 Chrome 由自动化控制时启用的功能(例如,在无头模式下运行或在 Selenium 控制下运行)。

(2)"PreciseMemoryInfo":禁用准确测量内存使用情况的能力。

(3)"PreciseTimeInfo":禁用准确测量时间的能力。

option = webdriver.ChromeOptions()
option.add_argument("--disable-blink-features=AutomationControlled")
browser = webdriver.Chrome(options=options)

1.3、Cookies 与 UA伪装

使用Cookies进行验证登录

cookies = {
    'domain': ".chat.openai.com",
    'name': '__Secure-next-auth.session-token',
    'value': 'xxx',
    'expires': 'Sat, 23-Dec-23 04:23:54 GMT',
    'path': '/',
    'HttpOnly': True,
    # 'HostOnly': True,
    'Secure': True,
    'SameSite': None
}
driver.get('https://chat.openai.com/chat')
driver.add_cookie(cookie_dict=cookies)

1.4、接管已打开的浏览器(以Chome为例)

启动一个新的浏览器会遇到许多问题,可选择直接接管已打开的浏览器

(1)在你的谷歌输入如下网址

chrome://version/

(2)找到可执行文件路径,就是你的驱动器安装地址,在CMD中输入

"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222

这时,Google浏览器会打开一个新窗口,之后在自己的程序中输入:

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
options = Options()
options.add_experimental_option("debuggerAddress", "localhost:9222")
 
driver = webdriver.Chrome(executable_path=r"C:\Users\THUNDEROBOT\PycharmProjects\douban\Mr.Ding\chromedriver.exe", chrome_options=options)

(3) 接管浏览器完成。

二、"--disable-blink-features"功能列表详细信息见

https://peter.sh/experiments/chromium-command-line-switches/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值