WebDriver FireFoxProfile UserAgent 使用 FireFoxDriver 进行切换

发布于 2025-01-01 12:58:22 字数 673 浏览 3 评论 0原文

我想知道是否可以动态更改用户代理配置文件,而无需创建新的 ForeFoxDriver 的实例?我有以下代码,我可以将其传递给 iphone 或 ipad 等的用户代理。它工作正常,但我必须为每个测试创建一个新实例,该实例在打开/关闭浏览器时速度很慢。例如,在使用 iPhone 用户代理进行测试后,我想将配置文件切换到 Android 用户代理或 iPad 用户代理,驱动程序是否可以选择更改此设置而无需创建新实例?

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override","some user agent, like iphone or iPad");
WebDriver driver = new FirefoxDriver(profile);

// do some tests
// set profile to new user agent
// profile.setPreference("general.useragent.override","some android user agent");

// can i now set the driver to the new profile here and perform more tests on this same instance of the driver?

I'm wondering if I can change the user agent profile on the fly, without creating a new
instance of the ForeFoxDriver? I have the following code, which I can pass in the user agent for iphone or ipad, etc.. it works fine, but i'm having to create a new instance for each test which is slow as it opens/closes the browser. E.g. After doing my tests with iPhone user agent, I'd like to switch the profile to Android user agent or iPad user agent, does the driver get the option to change this without needing to create a new instance?

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override","some user agent, like iphone or iPad");
WebDriver driver = new FirefoxDriver(profile);

// do some tests
// set profile to new user agent
// profile.setPreference("general.useragent.override","some android user agent");

// can i now set the driver to the new profile here and perform more tests on this same instance of the driver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凝望流年 2025-01-08 12:58:22

RemoteWebdriver(FirefoxDriver 的父类)仅在会话启动时(在构造函数中)更新功能映射。不幸的是我们无法在其他地方修改它。我认为这是一个非常好的主意。

RemoteWebdriver (parent class of FirefoxDriver) updates the capabilities map only when the session is starting (in the constructor). Unfortunately we can't modify it elsewhere. I think it is a very good idea.

因为看清所以看轻 2025-01-08 12:58:22

可能只有一种方法——通过扩展。但是,我从未尝试过这个...

FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.addExtension(new File("path/to/extension") );
WebDriver driver = new FirefoxDriver(ffProfile);

它可能使用的扩展是 User Agent Switcher:
https://addons.mozilla.org/cs/ firefox/addon/user-agent-switcher/?src=ss

但如前所述,我从未这样做过,也不知道如何通过 Selenium 控制插件

There is probably only one way - through extensions. However, I never tried this...

FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.addExtension(new File("path/to/extension") );
WebDriver driver = new FirefoxDriver(ffProfile);

The extension it might work with is User Agent Switcher:
https://addons.mozilla.org/cs/firefox/addon/user-agent-switcher/?src=ss

But as stated before, I never did that and have no clue how to control the addon through Selenium

沐歌 2025-01-08 12:58:22

在我们公司,我们使用 Browsermob 代理,这是一个用 java 编写的开源代理。它与 Selenium 完美集成,您可以从代码中控制它。它为我们提供多种用途,其中之一就是标头操作。

这里描述了这样做的方法: https://github.com/webmetrics/browsermob-proxy(HTTP 请求操作部分)

In our company we use Browsermob proxy, an open source proxy written in java. It integrates perfectly with Selenium, and you can control it from your code. It serves us for many purposes, one of them is header manipulation.

The way of doing it is described here: https://github.com/webmetrics/browsermob-proxy (HTTP Request Manipulation section)

习惯成性 2025-01-08 12:58:22

这对我来说工作:

server.addHeader("User-Agent", uaString)

This work for me:

server.addHeader("User-Agent", uaString)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文