0

我最近将我的 selenium 从 2.19.0 更新到了 2.31.0。我在设置 userAgent 以进行测试时遇到问题。这是代码:

void testMessage() {
    verifySomething(this.driver, "iPhone");
}

void verifySomething(WebDriver driver, String userAgent) {
    String script = null;
    if (driver instanceof HtmlUnitDriver) {
        script = "navigator.userAgent=" + "'" + userAgent + "';";
    }
    else {
        // something
    }
    ((JavascriptExecutor) driver).executeScript(script);
}

我收到以下错误:

======= 异常开始 ========

EcmaError: lineNumber=[1] column=[0] lineSource=[] name=[TypeError] sourceName=[injected script] message=[TypeError: Cannot set property [Navigator].userAgent that has only a getter to iPhone. (injected script#1)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot set property [Navigator].userAgent that has only a getter to iPhone. (injected script#1)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:663)
                at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:559)
                at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:525)
                at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:594)
                at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:569)
                at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:996)
                at org.openqa.selenium.htmlunit.HtmlUnitDriver.executeScript(HtmlUnitDriver.java:466)
...
...

== 调用 JAVASCRIPT ==

  function () {
      navigator.userAgent = "iPhone";
  }

======= EXCEPTION END ======== 

我知道它在抱怨二传手。它曾经在 selenium 2.19.0 中工作,但我猜他们可能已将属性更改为只读。

非常感谢任何帮助。

4

2 回答 2

0

您不能在 navigator.userAgent 中设置任何内容。

navigator.UserAgent 只是有关您当前浏览器的信息。

http://www.w3schools.com/jsref/prop_nav_useragent.asp

于 2013-04-04T19:59:50.740 回答
0

对于 FF 使用这个 (profile.setPreference("general.useragent.override", "your useragent") 一切都需要从 firefox 配置文件进行设置。

于 2013-04-08T12:17:04.180 回答