目录

背景

问题解决

完整代码

 附:查找的其他相关解决办法

参考链接:

背景

使用selenium实现自动登录,获取对应Cookie的值,可以方便我们对所需网页进行爬取,避免了使用requests爬取网页时时不时需要登录的问题。【1】

但使用使用selenium实现自动登录过程会遇到一些问题,如:

1.爬取时报错:ERROR:ssl_client_socket_impl.cc(975)] handshake failed; returned -1, SSL error code 1, net_error -101

2.进入网页后网页空白,错误代码400

3.谷歌浏览器提示“Chrome正在受到自动化测试软件的控制”

等等问题

以上三个问题为我实际操作过程遇到的问题,三个问题算同时出现,下面介绍解决办法:

问题解决

options=webdriver.ChromeOptions()

options.add_argument('--disable-blink-features=AutomationControlled')#关闭自动控制blink特征

加入关闭自动控制blink特征后就访问正常了,可以返回正确cookie。【2】

找了好多方法,但真正对我有用的就这个。实测采用参考4、7的方法无法关闭“受控”的提示语,但不影响正常登录。

完整代码

from selenium import webdriver

options=webdriver.ChromeOptions()

options.add_argument('--disable-blink-features=AutomationControlled')#关闭自动控制blink特征

b = webdriver.Chrome(options=options)

url = '' #自己的网页

b.get(url)

# 留足够长的时间给人工完成登录

#(完成登录的时候必须保证浏览器对象指向的窗口能够看到登录成功的效果)

input('已经完成登录:')

cookies = b.get_cookies()

with open('cookies.txt', 'w', encoding='utf-8') as f:

f.write(str(cookies))

with open('cookies.txt', encoding='utf-8') as f:

cookies = eval(f.read())

for x in cookies:

b.add_cookie(x)

b.get("")#如果登陆后有跳转是跳转后的网页

# 为了不让程序停止,给一个input指令

input()

(源码参考【1】) 

 附:查找的其他相关解决办法

# 忽略证书错误【3】【5】

options.add_argument('--ignore-certificate-errors')

#【6】

options.add_experimental_option("useAutomationExtension", False)

#忽略 Bluetooth: bluetooth_adapter_winrt.cc:1075 Getting Default Adapter failed. 错误【3】

options.add_experimental_option("excludeSwitches", ['enable-automation'])

#关闭提示语【4】【7】

options.add_argument('--disable-infobars')

options.add_argument('--disable-extensions')

options.add_argument('--disable-popup-blocking')

# 忽略 DevTools listening on ws://127.0.0.1... 提示【3】

options.add_experimental_option('excludeSwitches', ['enable-logging'])

#关闭自动控制连接blink特征【2】

options.add_argument('--disable-blink-features=AutomationControlled')

#禁用javaScript 来减少被识别为自动测试的风险【2】

options.add_argument('--disable-javascript')

参考链接:

【1】使用selenium如何实现自动登录_selenium自动登录_忧乐君的博客-CSDN博客

【2】python 如何消除chrome正受到自动软件测试的控制_mob64ca12f24f3a的技术博客_51CTO博客

 【3】14. 成功解决:ERROR:ssl_client_socket_impl.cc(992) handshake failed; returned -1, SSL error code 1, net_error -103-阿里云开发者社区 (aliyun.com)

 【4】selenium+python自动化87-Chrome正在受到自动软件的控制-腾讯云开发者社区-腾讯云 (tencent.com)

 【5】selenium解决ERROR:ssl_client_socket_impl.cc(962)] handshake failed; returned -1, SSL-CSDN博客

 【6】web自动化遇到问题总结-持续更新_chrome正在受到自动测试软件的控制-CSDN博客

【7】 selenium 使用chrome_driver自动化操作Google浏览器,调试的时候没有提示,但是编译后就提示一些错误的解决方法-CSDN博客

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。