使用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() # 记得要退出,否则会长久占用会话,下次无法连接
相关推荐
星星码️18 小时前
博客系统 Web 端测试报告:功能、自动化与性能
功能测试·测试工具·压力测试·模块测试·测试覆盖率
han688918 小时前
selenium之实战
笔记·python·selenium·测试工具·自动化
Madison-No71 天前
多语言聊天大模型--测试报告
linux·git·python·selenium·jmeter·自动化·postman
.又是新的一天.1 天前
AI接口测试03(操作数据库/管理文档/mock/自动化测试/定时任务/AI导师)
软件测试·测试工具·接口测试·apifox
Wokoo71 天前
自动化测试入门:核心场景、回归测试与 Web 自动化环境搭建
java·开发语言·测试工具
德思特1 天前
德思特混频器参数详解:5MHz–40GHz 宽频段与低噪声 LO 设计
测试工具·自动化·射频工程
wuyk5552 天前
《WiFi 嵌入式物联网开发全套实战》| 第 33 章 Wireshark 无线抓包环境搭建、802.11 帧过滤规则
物联网·测试工具·wireshark
落魄大学生之流水线上谋生计4 天前
Selenium 入门到实战:用 Python 写出稳定的浏览器自动化
python·selenium·自动化