当前位置: 首页 > news >正文

selenium使用已经打开的浏览器

Selenium 本身不支持直接连接到一个已经打开的浏览器页面。Selenium 启动的浏览器实例是一个全新的会话,它与手动打开的浏览器页面是分开的。但是,有一些变通的方法可以实现类似的效果。

一种方法是通过附加代理连接到已经打开的浏览器。下面是如何实现这一目标的步骤。

配置 Selenium WebDriver 以连接到现有的 Chrome 实例

  1. 启动 Chrome 浏览器时使用调试端口:

找到chrome的安装位置,执行命令:

chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\path\to\your\chrome\profile"
# --user-data-dir="C:\path\to\your\chrome\profile" 目录需要提前创建好
  1. 编写脚本连接到这个已经运行的 Chrome 实例:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By# 配置 ChromeDriver 的选项以连接到已经运行的 Chrome 实例
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")# 获取当前脚本的目录
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
chrome_driver_path = os.path.join(current_dir, 'chromedriver')# 设置 ChromeDriver 的服务
service = Service(chrome_driver_path)# 启动 WebDriver 并连接到现有的 Chrome 实例
driver = webdriver.Chrome(service=service, options=chrome_options)# 现在你可以使用 Selenium 控制已经打开的 Chrome 实例
driver.get("https://www.baidu.com")# 打印网页标题
print(driver.title)# 关闭浏览器
driver.quit()

目录层级结构

|-chromedriver
└── subfolder/└── your_script.py

详细步骤

  1. 启动 Chrome 浏览器:以调试模

详细步骤

  1. 启动 Chrome 浏览器:以调试模式启动 Chrome 浏览器,使其监听指定端口。
chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\path\to\your\chrome\profile"

确保 Chrome 的可执行文件路径正确,并指定一个用户数据目录,以便保留浏览器状态。

  1. 编写脚本连接到已经运行的 Chrome 实例
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options# 配置 ChromeDriver 的选项以连接到已经运行的 Chrome 实例
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")# 获取当前脚本的目录
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
chrome_driver_path = os.path.join(current_dir, 'chromedriver')# 设置 ChromeDriver 的服务
service = Service(chrome_driver_path)# 启动 WebDriver 并连接到现有的 Chrome 实例
driver = webdriver.Chrome(service=service, options=chrome_options)# 现在你可以使用 Selenium 控制已经打开的 Chrome 实例
driver.get("https://www.baidu.com/")# 打印网页标题
print(driver.title)# 保持浏览器打开,等待用户手动关闭
input("Press Enter to close the browser...")# 关闭浏览器
driver.quit()

解释

  1. 启动 Chrome 浏览器

    chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\path\to\your\chrome\profile"
    
    • --remote-debugging-port=9222:指定 Chrome 浏览器的远程调试端口。
    • --user-data-dir="C:\path\to\your\chrome\profile":指定 Chrome 的用户数据目录。
  2. 配置 ChromeDriver 的选项

    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    
    • 通过调试地址 127.0.0.1:9222 连接到已经运行的 Chrome 实例。
  3. 启动 WebDriver 并连接到现有的 Chrome 实例

    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    • 使用指定的调试地址启动 WebDriver。

通过这种方式,你可以让 Selenium 控制一个已经打开的 Chrome 浏览器实例,从而在现有会话中执行自动化任务。

http://www.lryc.cn/news/373041.html

相关文章:

  • Redis: 深入解析高性能内存数据库的实现原理
  • 使用 Python进行自动备份文件
  • 02_01_SpringMVC初识
  • Python学习打卡:day04
  • gitlab问题记录
  • OpenCV练习(1)签名修复
  • 软设之系统测试之测试的基本概念及分类
  • Python学习打卡:day06
  • 支付宝 沙盒demo使用
  • ConcurrentHashMap如何保证线程安全?
  • spring属性注入的不细心错误
  • JVM 根可达算法
  • Kafka基础架构与核心概念?有哪些应用场景?
  • 内网不能访问网站怎么办?
  • python-求f(x,n)
  • java值jsp语法笔记
  • 057、PyCharm 运行代码报错:Error Please select a valid Python interpreter
  • Java实现图书管理系统
  • 使用静态方法接受对象参数
  • cocos creator如何使用cryptojs加解密(及引入方法)
  • 安装台式电脑网卡驱动
  • JavaEE-多线程(1)
  • 【计算机视觉】人脸算法之图像处理基础知识(五)
  • 工业 web4.0 的 UI 风格,独树一帜
  • BSP驱动教程-CAN/CANFD/CANopen知识点总结分享
  • 微服务之远程调用
  • Opencv数一数有多少个水晶贴纸?
  • AI Agent智能应用从0到1定制开发(完结)
  • 事件驱动架构:新时代的软件设计范式
  • 【机器学习】机器学习与物流科技在智能配送中的融合应用与性能优化新探索