我想用selenium打开一个网站
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.44.0</version>
</dependency>
我的Java代码如下所示:
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("https://somewebsite.com");
我得到以下异常:
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function getElementsByClassName in object [object HTMLDivElement]. (https://somewebsite.com/Layout/General/lic-notification.js#245)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:705)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:637)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:612)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:1001)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventListeners(EventListenersContainer.java:179)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:239)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:824)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:748)
at com.gargoylesoftware.htmlunit.html.HtmlElement$1.run(HtmlElement.java:920)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:925)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1298)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:290)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:475)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:342)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:407)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:480)
在我看来,getElementByClassname函数没有在selenium的JS引擎中定义,这与网络上的几个帖子形成对比,在这些帖子中使用此函数来访问隐藏的输入元素。 我做错了什么还是硒中的错误?
编辑: 这是导致崩溃的JS函数。我没有自己写,我只是访问网站做一些重复的任务。
var getPendingContainter = function () {
var e, d;
if (!(e = document.getElementById("nPendingContainer"))) {
e = ce("div"); sa(e, "id", "nPendingContainer");
sa(e, "style", "display:none;");
ac(document.body, e);
m = ce("div"); cn(m, "marker"); ac(e, m);
d = ce("div"); cn(d, "inner"); ac(e, d);
} else {
d = document.getElementById("nPendingContainer").getElementsByClassName("inner")[0];
}
return d;
}
EDIT2: 调试站点显示,jQuery使用.ready()函数调用此函数。我不知道它是否有帮助。我的Chrome不会给我任何错误。