使用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() # 记得要退出,否则会长久占用会话,下次无法连接
相关推荐
小白学大数据44 分钟前
长周期爬虫的数据一致性:断点续爬 + 事务回滚保障采集质量
开发语言·爬虫·测试工具
小绫网络安全10 小时前
Wireshark抓包完全入门教程:从零开始掌握网络分析
arm开发·测试工具·wireshark
梦想不只是梦与想1 天前
鸿蒙 测试工具:DevEco Testing(一)
测试工具·harmonyos·testing
ClouGence1 天前
Selenium 写不动了?这个工具录一次就能跑 Web 自动化
前端·selenium·测试
fthux1 天前
不必下载整个仓库:GitZip Pro 让 GitHub 文件与文件夹批量下载更简单
前端·chrome·ai·edge·开源·github·firefox
我的xiaodoujiao2 天前
快速学习Python基础知识详细图文教程17--类型注解和断点调试
开发语言·python·学习·测试工具
泽众云测试2 天前
泽众软件与日联科技达成合作,TestOne自动化测试平台助力搭建工业检测装备数字化品控防线
功能测试·测试工具·自动化
xy34532 天前
SQLMap 核心语法与使用指南(三)
测试工具·渗透测试·sqlmap
我的xiaodoujiao2 天前
快速学习Python基础知识详细图文教程18--多线程
开发语言·python·学习·测试工具
匠测AI说2 天前
Selenium 工作原理深度解析:从架构到实践
selenium·测试工具·架构