惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
U
Unit 42
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
Hacker News: Ask HN
Hacker News: Ask HN
Vercel News
Vercel News
S
Security @ Cisco Blogs
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
I
Intezer
MongoDB | Blog
MongoDB | Blog
AI
AI
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
W
WeLiveSecurity
博客园 - 叶小钗
S
SegmentFault 最新的问题
N
News | PayPal Newsroom
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
DataBreaches.Net
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
H
Help Net Security
美团技术团队
博客园 - 司徒正美
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Vulnerabilities – Threatpost
TaoSecurity Blog
TaoSecurity Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity
J
Java Code Geeks
量子位
Martin Fowler
Martin Fowler
博客园_首页

阿猪的博客

如何使用CNB构建Hexo项目并部署到腾讯云COS 好坑爹!派息后买入的股票怎么也扣股息税? 游哈尔滨 跨年往事 安装Ta-Lib时报错"Could not build wheels" 网站被恶意镜像了该怎么办 延迟退休正式到来!来看看有什么规定 我的基金怎么就被强制卖出了? Hexo-AlgoliaSearch插件报错“Error has occurred during indexing posts” 找工作避坑不完全指南 办理信用卡应该注意什么 华为Pad开启了“共享至电脑”,但是Windows下无法正常访问 webdriver-manager报错一例 Windows无法从环境变量中找到Python的正确位置 习大大2024年新年贺词 号外!号外!Twikoo可以直接屏蔽垃圾评论啦! 如何将Coding的代码仓同步到Github 博客被攻击了 华为Pad开启了“共享至电脑”,但是Ubuntu下无法正常访问 如何恢复Etcher刻录过的U盘 如何在Ubuntu下快速切换网络代理状态 在腾讯云函数中使用Pandas报错`No module named 'numpy.core._multiarray_umath'` 对空的DataFrame使用apply方法未得到预期结果 SQLite中使用关键字作为列名称导致报错 Logging模块重复输出内容的原因及解决方法 如何下载与Chrome浏览器的版本相匹配的ChromeDriver 如何下载旧版本Python的安装包 如何为不同的Python项目自动选择不同的解释器 使用腾讯云函数搭建Web站点 WordPress CORS问题一例 踩坑阿里云函数计算搭建WordPress 在AWS Amplify中部署Jekyll站点 让Chirpy主题支持折叠展示代码块 实现ChatGPT的文字输出效果 python中使用'''注释代码后引起报错 使用python自带的email模块解析邮件 使用pandas_bokeh在地图上显示数据 如何通过复权因子计算复权价格 国内量化平台不完全汇总
如何在Selenium中保持网站的登录状态
阿猪 · 2023-08-27 · via 阿猪的博客

一、问题场景

  Selenium是一个广泛使用的Web自动化测试工具,可以通过Python控制浏览器模拟各种用户操作。在默认情况下,Selenium打开的浏览器与原生的浏览器在变量环境上是相互隔离的,用户无法继承原生浏览器中的网站登录状态,每次运行时都需要重新进行登录,很不方便。
  经过在ChatGPT、度娘上一通搜索和尝试,阿猪找到一个操作简便的实现方法,就是为Selenium指定浏览器的User Data目录,与原生的浏览器共享cookie、缓存等文件。

二、为Selenium指定浏览器的User Data目录

  本文以Window和Chrome环境为例,示例代码如下:

1
2
3
4
5
from selenium import webdriver

obj_option = webdriver.ChromeOptions()
obj_option.add_argument(r"user-data-dir=C:\Users\zhu\AppData\Local\Google\Chrome\User Data")
obj_page = webdriver.Chrome(options=obj_option)

  这里假设你已经解决了WebDriver与浏览器的适配问题。
  需要注意的是,在运行的时候Windows中不要同时打开其他的Chrome浏览器窗体实例,否则可能会遇到如下报错:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally.
(unknown error: DevToolsActivePort file doesn’t exist)

版权声明: 未经书面授权许可,任何个人和组织不得以任何形式转载、引用本站的任何内容。本站保留追究侵权者法律责任的权利。

赏杯咖啡,鼓励一下~

  • 微信打赏

    微信打赏