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

相关推荐
写代码的【黑咖啡】7 小时前
Python中的Selenium:强大的浏览器自动化工具
python·selenium·自动化
程序员雷叔16 小时前
在postman设置请求里带动态token,看看这两种方法!
selenium·测试工具·单元测试·测试用例·pytest·lua·postman
CeshirenTester2 天前
从Selenium到Playwright:快速入门指南
selenium·测试工具
半路_出家ren4 天前
20.基于Selenium实现界面自动化控制
运维·python·selenium·测试工具·网络安全·自动化·chromedriver
_OP_CHEN4 天前
【测试理论与实践】(十)Web 项目自动化测试实战:从 0 到 1 搭建博客系统 UI 自动化框架
运维·自动化测试·python·测试开发·selenium·自动化·测试开发工程师
未定义.2215 天前
第1篇:0基础入门!Python+Selenium环境搭建与第一个自动化脚本
python·功能测试·selenium·自动化·jenkins·pytest
李昊哲小课5 天前
Selenium 自动化测试教程
爬虫·selenium·测试工具
cnxy1885 天前
Python爬虫进阶:反爬虫策略与Selenium自动化完整指南
爬虫·python·selenium
阿蔹5 天前
Session与Cookies
selenium·测试
今晚务必早点睡6 天前
Playwright 实战全解析:使用方法、典型场景及与 Selenium 的全面对比
selenium·测试工具·playwright