Selenium库打开指定端口(9222、9333等)浏览器【已解决!!!】

就是在写动态爬虫爬取数据的过程中,如果用selenium的话,有一个缺点,就是当我们去测试一个网站能不能爬取,它都会重新换端口打开一个浏览器,不会使用上一次使用的浏览器,在实际使用过程中这样调试很烦,总是会重新打开而不是就用上一次打开过的调试。

所以我就在想,怎么让它每一次都打开同一个浏览器呢?

刚开始,借鉴网上都是这种:

复制代码
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
#设置端口
opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=opt)
url='https://www.baidu.com/'
# 访问目标网页
driver.get(url)
time.sleep(100000)

但是我运行这种会报错,浏览器迟迟不打开,然后抛出

selenTraceback (most recent call last):
File "E:\pythonProject14\Selenium动态爬虫\Selenium指定端口打开浏览器.py", line 9, in <module>
File "E:\python3.9.13\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in init
super().init(
File "E:\python3.9.13\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in init
super().init(command_executor=executor, options=options)
File "E:\python3.9.13\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 250, in init
self.start_session(capabilities)
File "E:\python3.9.13\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 342, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "E:\python3.9.13\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute
self.error_handler.check_response(response)
File "E:\python3.9.13\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)ium.common.exceptions.SessionNotCreatedException: Message: session not created: cannot connect to chrome at 127.0.0.1:9222
from chrome not reachable

后来找到一种解决办法

就是手动打开这个端口的浏览器在运行。然后它就可以成功!!!!

但是手动打开麻烦,我就写成自动的。

好,我把代码分享给大家

1.打开指定端口浏览器.py

复制代码
import subprocess

# 定义要执行的命令
chrome_path = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"'  # 使用原始字符串避免转义
debugging_port = "--remote-debugging-port=9222"
user_data_dir = r'--user-data-dir="E:\Selenium调试"'        #要提前新建一个文件夹

# 构建完整的命令
command = f"{chrome_path} {debugging_port} {user_data_dir}"

# 执行命令
try:
    subprocess.Popen(command, shell=True)  # 使用 subprocess.Popen 启动进程
    print("Chrome started with remote debugging enabled.")
except Exception as e:
    print(f"An error occurred: {e}")

2.运行打开指定端口浏览器.py

复制代码
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=opt)
url='https://www.baidu.com/'
# 访问目标网页
driver.get(url)
time.sleep(100000)

先运行1,在运行2.

就可以实现打开指定端口浏览器的效果了

12如果写在一起会出现一些多余内容。我是分开写的

相关推荐
水银嘻嘻2 小时前
web 自动化之 selenium 下拉&鼠标键盘&文件上传
selenium·自动化
攻城狮7号6 小时前
Python爬虫第20节-使用 Selenium 爬取小米商城空调商品
开发语言·数据库·爬虫·python·selenium
代码的乐趣1 天前
支持selenium的chrome driver更新到136.0.7103.92
chrome·python·selenium
xixixiLucky2 天前
配置Java Selenium Web自动化测试环境
java·前端·selenium
熊文豪3 天前
Java+Selenium+快代理实现高效爬虫
java·爬虫·selenium·隧道代理·快代理
水银嘻嘻4 天前
web 自动化之 selenium+webdriver 环境搭建及原理讲解
前端·selenium·自动化
水银嘻嘻4 天前
web 自动化之 Selenium 元素定位和浏览器操作
前端·selenium·自动化
测试老哥5 天前
Selenium使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
xixixiLucky5 天前
Selenium Web自动化测试学习笔记(一)
笔记·学习·selenium
可了~5 天前
使用 Selenium 截图功能,截不到原生 JavaScript 弹窗
selenium·测试工具