我最近将我的 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 中工作,但我猜他们可能已将属性更改为只读。
非常感谢任何帮助。