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如果写在一起会出现一些多余内容。我是分开写的

相关推荐
测试19983 天前
Selenium自动化测试框架的搭建
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
AndreasEmil4 天前
基于多设计模式的抽奖系统 - 测试报告
java·selenium·设计模式·postman
tiger从容淡定是人生5 天前
Selenium与Playwright:两大Web自动化框架的深入对比
前端·selenium·测试工具·自动化·web测试·playwright·信息化战略
Cd ...8 天前
RobotFramework Selenium与Browser常用关键字对比
自动化测试·selenium·robotframework·playwright·browser
qq_452396239 天前
第二篇:《主流UI自动化工具横向对比:Selenium、Cypress、Playwright、Puppeteer》
selenium·ui·自动化
迷藏49410 天前
# 发散创新:基于Selenium的自动化测试框架重构与实战优化在当今快速迭代的软件开
java·python·selenium·测试工具·重构
willhuo10 天前
# 自动化数据采集技术研究与实现:基于Playwright的抖音网页自动化方案
运维·selenium·c#·自动化·chrome devtools·webview