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

相关推荐
小陈的进阶之路5 小时前
Selenium元素定位
python·selenium
紫丁香8 小时前
Selenium自动化测试详解1
python·selenium·测试工具·ui
爱敲代码的菜菜1 天前
【测试】自动化测试
css·selenium·测试工具·junit·自动化·xpath
爱敲代码的菜菜1 天前
【测试】Selenium
selenium·测试工具·xpath·webdriver·cssselector
若惜2 天前
selenium自动化测试web自动化测试 框架封装Pom
前端·python·selenium
念越3 天前
自动化测试入门指南:Selenium环境搭建+第一个实战案例
自动化测试·selenium·测试工具·蓝桥杯
小邓睡不饱耶3 天前
实战教程:基于Selenium+BeautifulSoup爬取易车网新能源汽车销量数据
selenium·测试工具·beautifulsoup
sheepfagdng3 天前
Python-web自动化-selenium(2)
运维·selenium·自动化
虚幻如影4 天前
Selenium 自动化测试中 Chrome 浏览器弹出“您的连接不是私密连接”
chrome·selenium·测试工具
sheepfagdng4 天前
python-web自动化-selenium(1)
selenium·测试工具