java 为 HtmlUnitDriver Selenium 设置用户代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12853715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:29:35  来源:igfitidea点击:

Setting User Agent for HtmlUnitDriver Selenium

javaseleniumselenium-webdriver

提问by Jeffrey Chen

How do I set the user agent property for HtmlUnitDriver in Selenium Java ? I can set it for the firefox driver with

如何在 Selenium Java 中为 HtmlUnitDriver 设置用户代理属性?我可以为 Firefox 驱动程序设置它

FirefoxProfile ffp = new FirefoxProfile();
ffp.setPreference("general.useragent.override", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; Media Center PC 6.0; InfoPath.3; MS-RTC LM 8; Zune 4.7");
WebDriver driver = new FirefoxDriver(ffp);

Is there a way to do this for HtmlUnitDriver ? I've tried to use the setCapability("UserAgentName", "some UA settings"); but this does not work.

有没有办法为 HtmlUnitDriver 做到这一点?我尝试使用 setCapability("UserAgentName", "some UA settings"); 但这不起作用。

回答by aradhak

Did you try using DesiredCapabilities?

您是否尝试过使用 DesiredCapabilities?

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setBrowserName(<browser_name>);
capabilities.setPlatform(<platform>);
capabilities.setVersion(<version>);
driver = new HtmlUnitDriver(capabilities);

回答by Lukas

Setting custom User Agent string for HtmlUnitDriver:

HtmlUnitDriver设置自定义用户代理字符串:

final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20160101 Firefox/66.0";

WebDriver driver = new HtmlUnitDriver(new BrowserVersion(
                "Firefox", "5.0 (Windows)", USER_AGENT, 66 //important is 3rd argument
));

? ??

? ??

It works. I tested it on http://myhttp.infofor getting user agent from remote server

有用。我在http://myhttp.info上测试了它以从远程服务器获取用户代理

(OS: W7, Selenium version: 2.37.1, Java 7u45 x64)

(操作系统:W7,Selenium 版本:2.37.1,Java 7u45 x64)

@Test
public void testUserAgent() throws Exception {
    driver.get("http://myhttp.info");
    MyHttpInfoPage myHttpInfoPage = PageFactory.initElements(driver, MyHttpInfoPage.class);
    String userAgent = myHttpInfoPage.getUserAgent(); // @FindBy(xpath = "//td[text()='User agent']/following-sibling::td")
    Assert.assertEquals(userAgent, USER_AGENT);
}


(see also BrowserVersionJavaDoc)

(另见BrowserVersionJavaDoc)

回答by acdcjunior

HtmlUnit has recently updated their internalsregarding browser versions. Now to change the emulated browser version, which sets the user-agent string, you have to specify the browser versioncapability, such as:

HtmlUnit最近更新了有关浏览器版本的内部信息。现在要更改设置用户代理字符串的模拟浏览器版本,您必须指定浏览器version功能,例如:

Chrome (default)

铬(默认)

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion(org.openqa.selenium.remote.BrowserType.CHROME);
WebDriver myDriver = new HtmlUnitDriver(capabilities);

Internet Explorer

IE浏览器

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion(org.openqa.selenium.remote.BrowserType.IE);
WebDriver myDriver = new HtmlUnitDriver(capabilities);

Firefox (no firefox version specified, defaults to 52)

Firefox(未指定 firefox 版本,默认为 52)

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setVersion(org.openqa.selenium.remote.BrowserType.FIREFOX);
WebDriver myDriver = new HtmlUnitDriver(capabilities);

Firefox (specifying one of the two available versions: 45 or 52)

Firefox(指定两个可用版本之一:45 或 52)

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
// specify ff version 45
capabilities.setVersion(org.openqa.selenium.remote.BrowserType.FIREFOX + "-45");
// ...or specify ff version 52
capabilities.setVersion(org.openqa.selenium.remote.BrowserType.FIREFOX + "-52");
WebDriver myDriver = new HtmlUnitDriver(capabilities);

The above are all of the available versions as of today.

以上是截至今天的所有可用版本。

To verify if they changed, check their master branch.

要验证它们是否更改,请检查它们的 master 分支