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

相关推荐
kebeiovo21 小时前
常用的几种测试工具:selenium,jmeter,jenkins
selenium·测试工具·jmeter
小白学大数据1 天前
应对反爬:使用Selenium模拟浏览器抓取12306动态旅游产品
selenium·测试工具·旅游
程序员的世界你不懂1 天前
【框架】基于selenium+java框架设计(0-1实战)
java·selenium·servlet
aiden:)2 天前
Selenium WebUI 自动化“避坑”指南——从常用 API 到 10 大高频问题
开发语言·前端·javascript·python·selenium
魏波.3 天前
UI自动化测试之Selenium元素定位8大方式
selenium·测试工具·ui
FL16238631293 天前
windows下selenium的chromedriver安装和环境变量的配置
windows·selenium·测试工具
烟雨迷3 天前
web自动化测试(selenium)
运维·开发语言·前端·python·selenium·测试工具
RUNNING123!3 天前
browsermobproxy + selenium 获取接口json
selenium·测试工具·json
BatyTao3 天前
Selenium核心技巧:元素定位与等待策略
selenium·测试工具
公众号:重生之成为赛博女保安4 天前
一款基于selenium的前端验证码绕过爆破工具
前端·selenium·测试工具