当前位置: 首页 > news >正文

使用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 

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import KeysGECKODRIVER_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:

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import ServiceGECKODRIVER_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实例:

geckodriver --connect-existing --marionette-port 2828

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

例子如下:Python3 + Selenium 4

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 Optionsdriver = 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,内容如下:

[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

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 Optionsoptions = 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() # 记得要退出,否则会长久占用会话,下次无法连接

http://www.dtcms.com/a/494785.html

相关文章:

  • 普蓝机器人PlanRobot-DR200:基于多传感融合的全天候电力巡检自主导航技术与实践
  • PHPCMS V9 自定义证书查询模块(Ajax+防刷+倒计时)
  • 一体化运维平台:当下运维体系的核心支柱
  • HarmonyOS后台任务管理:短时任务与长驻任务实战
  • Unity游戏基础-6(跨平台生成游戏作品,针对安卓教程)
  • Luminex xMAP技术原理与应用概述
  • Http基础协议和解析
  • 官方网站页面尺寸html网页设计作品中国传统文化
  • h5游戏免费下载:激射神经猫
  • 商业航天与数字经济(二):商业航天重构全球数字经济的底层逻辑
  • 免费社区建站系统vue做的商城网站
  • 中电金信:首个金融信创中试平台揭牌,架设国产软硬件落地应用的“高速通道”
  • 主流移动通信标准
  • SNK施努卡驱动电机自动生产线,转子+电机总成含EOL测试
  • 解决方案 - 宽带多通道同步采集系统
  • Nginx、uwsgi、uWSGI、WSGI和Django的关系
  • 牛童三国单机游戏Unity源码 免费开源
  • 团工作网站建设意见网站编辑做图片用什么不同
  • C#,VB.NET数组去重复,提取键名和重复键和非重复键
  • java建筑工地智能交互平台源码,智慧工地SaaS云平台,对现场人员、材料、机械、质量、安全进行高效管控
  • 【计算机网络笔记】计算机网络学习笔记1
  • 企业网站添加栏目龙华建网站多少钱
  • 建安证查询网站制作二维码网站免费
  • iOS八股文之 内存管理
  • Rhino 8 for Mac 犀牛3D建模软件
  • IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
  • 数字化转型从来不是上套系统那么简单
  • 探索 MaynorAI:你的一站式 AI 服务平台
  • 购物网站建设ppt全国网站建设公司
  • 实现机器人大小脑深度融合:研华科技与国讯芯微联合发布Thor平台具身智能控制器