首页 > 解决方案 > Selenium WebDriver - Java - 更改用户代理

问题描述

我目前正在 selenium wbesite 上浏览 selenium webdriver。

网站的一部分讨论了“更改用户代理”并解释了如何做到这一点:https ://docs.seleniumhq.org/docs/03_webdriver.jsp#chapter03-reference但是,没有“真实”或“模拟”示例在“动作”中显示了这一点。

所以,我想知道这里是否有人可能拥有或能够共享包含该项目的代码片段,显示这个“在行动中”?一个模拟项目,您可能拥有或设计自己来实践这一点,就足够了。

我已经在网上做了很多研究,试图找到一个这样的例子,但没有成功。

标签: javaseleniumselenium-webdriver

解决方案


从您提供的链接中,您可以User-Agent使用以下代码更改:

火狐:

FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "user-agent-string");
WebDriver driver = new FirefoxDriver(profile);

铬合金:

ChromeOptions chrome = new ChromeOptions();
chrome.addArguments("user-agent=YOUR_USER_AGENT");

Java你可以轻松切换到任何支持 selenium 的语言,因为它们都实现FirefoxProfileChromeOptions


推荐阅读