使用Selenium Server 4连接已经运行的Firefox

使用Python + Selenium4可以直接连接已经打开的Firefox实例,并控制它访问网站。

准备工作:使用marionette(木偶)模式启动Firefox

Firefox启动的时候,需要启动为木偶模式(可以通过网络端口进行远程控制的模式)。木偶模式的默认端口是2828。

要启动木偶模式,可以在firefox启动时,添加以下参数:

-marionette

对于旧版的firefox,还可以使用以下参数更改远程控制的默认端口

-start-debugger-server <PORT>

但是,对于新版的firefox,不能时候上面的参数更改端口号,而需要直接修改firefox的配置信息。方法如下:

打开firefox,在地址栏输入 about:config并回车,搜索 marionette.port。可以看到,默认的参数值是2828。可以更改为其它未占用的端口号,但是要注意python程序中的对应端口号也需要同时更改

方法1:直接使用 Firefox()实例连接

客户端参考代码如下:Python 3+Selenium3

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

GECKODRIVER_PATH = r'./geckodriver.exe'
driver = webdriver.Firefox(executable_path = GECKODRIVER_PATH, service_args = ['--marionette-port', '2828', '--connect-existing'] )
 
driver.get('https://www.baidu.com')
print(driver.title)

以下适用于 Python3 + Selenium 4:

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service

GECKODRIVER_PATH = r'./geckodriver.exe'
driver = webdriver.Firefox(service = Service(executable_path = GECKODRIVER_PATH, service_args =['--marionette-port', '2828', '--connect-existing'] ))
 
driver.get('https://www.baidu.com')
print(driver.title)

方法2:使用Remote()实例连接

首先启动geckodriver并连接到已经启动的 Firefox实例:

bash 复制代码
geckodriver --connect-existing --marionette-port 2828

这样,驱动会在4444端口建立一个远程调用端口,可以利用Remote()实例进行连接

例子如下:Python3 + Selenium 4

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

driver = webdriver.Remote("http://127.0.0.1:4444", options = Options())
 
driver.get('https://www.baidu.com')
print(driver.title)

方法3:使用Remote()实例,并通过Selenium Server Grid 4进行连接

也需要首先运行Firefox和geckodriver。但是,因为Grid Server默认端口也是4444,而且修改比较繁琐,建议更改geckodriver的监听端口。

geckodriver --connect-existing --marionette-port 2828 -p 3002

这样,将监听端口更改为3002

然后,启动Selenium Server作为hub

命令如下:

java -jar selenium-server-4.36.0.jar hub

然后先生成一个node的配置文件 relay.toml,内容如下:

python 复制代码
[node]
detect-drivers = false

[relay]
# Default Appium/Cloud server endpoint
url = "http://localhost:3002"
status-endpoint = "/status"
# Optional, enforce a specific protocol version in HttpClient when communicating with the endpoint service status (e.g. HTTP/1.1, HTTP/2)
protocol-version = "HTTP/1.1"
# Stereotypes supported by the service. The initial number is "max-sessions", and will allocate 
# that many test slots to that particular configuration
configs = [
  "5", "{\"browserName\": \"firefox\", \"platformName\": \"WINDOWS\", \"browserVersion\": \"102.0.1\", \"networkname:applicationName\":\"node_1\", \"nodename:applicationName\":\"app_1\"}"
]

启动Node:

java -jar selenium-server-4.36.0.jar node --config relay.toml --port 5151

如果只启动一个Node,那么可以不修改端口号

测试程序如下:Python3 + Selenium 4

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

options = Options()
# options.add_argument("--headless") #设置火狐为headless无界面模式
# options.add_argument("--disable-gpu")
# options.capabilities["browserVersion"] = "102.0.1"
options.set_capability("se:name", "My simple test"); 
options.set_capability("networkname:applicationName", "node_1")
options.set_capability("nodename:applicationName", "app_1")
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", options = options)
 
driver.get('https://www.baidu.com')
print(driver.title)

driver.quit() # 记得要退出,否则会长久占用会话,下次无法连接
相关推荐
测试工程师成长之路1 天前
Serenity BDD 框架:Java + Selenium 全面指南(2026 最新)
java·开发语言·selenium
may_一一1 天前
xpath定位:selenium和playwrightAnt Design / 表单类页面)
selenium·测试工具
daopuyun1 天前
CNAS/CMA软件检测实验室源代码漏洞测试工具选型要求与比对
软件测试·测试工具·软件检测·cnas认可·cma认定
Wpa.wk1 天前
接口自动化测试 - 请求构造和响应断言 -Rest-assure
开发语言·python·测试工具·接口自动化
AI_56781 天前
Postman接口测试提速技巧:批量请求+智能断言实践
测试工具·lua·postman
Luminbox紫创测控1 天前
整车自然暴晒与全光谱阳光模拟老化相关性研究
测试工具
弹简特1 天前
【JavaEE06-后端部分】SpringMVC01-Spring MVC第一大核心URL 路由映射【建立连接】与 Postman 接口测试详解
java·spring boot·测试工具·spring·postman
0思必得02 天前
[Web自动化] Selenium设置相关执行文件路径
前端·爬虫·python·selenium·自动化
测试大圣2 天前
软件测试基础知识总结(超全的)
软件测试·python·功能测试·测试工具·职场和发展·单元测试·测试用例