使用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() # 记得要退出,否则会长久占用会话,下次无法连接
相关推荐
程序员念姐6 小时前
软件测试系统流程和常见面试题
测试工具·面试
Hacker_Oldv6 小时前
Python技能进阶:探索Selenium库,实现网页自动化测试与爬虫
自动化测试·软件测试·爬虫·python·selenium·职场和发展
西游音月8 小时前
(5)pytest+Selenium自动化测试-元素定位之XPath定位
selenium·测试工具·pytest
L.Ru9 小时前
SIP抓包工具-sngrep的使用
测试工具·信息与通信·sngrep
卓码软件测评20 小时前
第三方软件测评机构:【Gatling与JMeter的架构对比:异步与非阻塞I/O模型如何实现更高并发】
测试工具·jmeter·架构·测试用例·负载均衡·压力测试
美团程序员20 小时前
一篇文章教你搞定:”xx 功能如何测试?“常见面试题型!
测试工具·面试·职场和发展·测试用例
软件测试雪儿1 天前
高频Postman软件测试面试题
测试工具·lua·postman
2501_915106321 天前
iOS App 测试工具全景分析,构建从开发调试到线上监控的多阶段工具链体系
android·测试工具·ios·小程序·uni-app·iphone·webview
小白学大数据1 天前
构建混合爬虫:何时使用Requests,何时切换至Selenium处理请求头?
爬虫·python·selenium·测试工具
Hacker_Fuchen1 天前
POST请求提交数据的三种方式及通过Postman实现
自动化测试·软件测试·测试工具·postman