2015-05-09 40 views
2

當某個網站(driver.get("http://steamcommunity.com/market/search?appid=730"))使用driver.get工作,我得到這個錯誤:driver.get不是在硒的webdriver,爪哇

Exception in thread "main" org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking fireEvent 
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50' 
System info: host: 'daltonpc', ip: '10.0.0.2', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45' 
Driver info: driver.version: HtmlUnitDriver 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:504) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:480) 
    at org.openqa.selenium.example.FinderClass.findOtherWeapon(FinderClass.java:60) 
    at org.openqa.selenium.example.Main.main(Main.java:20) 
Caused by: com.gargoylesoftware.htmlunit.ScriptException: Exception invoking fireEvent 
    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:491) 
    ... 3 more 
Caused by: java.lang.RuntimeException: Exception invoking fireEvent 
    at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:148) 
    at net.sourceforge.htmlunit.corejs.javascript.FunctionObject.call(FunctionObject.java:448) 
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1531) 
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798) 
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105) 
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411) 
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309) 
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3057) 
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:103) 
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:630) 
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:690) 
    ... 22 more 
Caused by: com.gargoylesoftware.htmlunit.ScriptException: illegal radix 0. (http://steamcommunity-a.akamaihd.net/public/shared/javascript/shared_global.js?v=BESEFoKTgss6&l=english#1358) 
    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:814) 
    at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:748) 
    at com.gargoylesoftware.htmlunit.javascript.host.EventNode.fireEvent(EventNode.java:396) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:120) 
    ... 32 more 
Caused by: net.sourceforge.htmlunit.corejs.javascript.EvaluatorException: illegal radix 0. (http://steamcommunity-a.akamaihd.net/public/shared/javascript/shared_global.js?v=BESEFoKTgss6&l=english#1358) 
    at com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter.runtimeError(StrictErrorReporter.java:81) 
    at net.sourceforge.htmlunit.corejs.javascript.Context.reportRuntimeError(Context.java:1047) 
    at net.sourceforge.htmlunit.corejs.javascript.Context.reportRuntimeError(Context.java:1094) 
    at net.sourceforge.htmlunit.corejs.javascript.Context.reportRuntimeError1(Context.java:1062) 
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.numberToString(ScriptRuntime.java:813) 
    at net.sourceforge.htmlunit.corejs.javascript.NativeNumber.execIdCall(NativeNumber.java:129) 
    at net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject.call(IdFunctionObject.java:89) 
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1531) 
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798) 
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105) 
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:630) 
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:690) 
    ... 47 more 

它正常工作與其他URL,也與此一合作直到我啓用JavaScript的trueWebDriver driver = new HtmlUnitDriver(true);。我需要這樣做是因爲我的程序在沒有javascript的情況下無法正常工作,並且看起來像這個蒸汽社區市場頁面需要JavaScript才能訪問它,即使只需輸入url。我究竟做錯了什麼?

+0

只是一個建議,但嘗試使用類似PhantomJSdriver,我發現相比Htmluntidriver那是相當穩定的。 – LittlePanda

+0

你需要提一下你想要模擬的瀏覽器。所以當你初始化驅動程序時,只需添加'HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME);'FYI:http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/BrowserVersion.html – stanjer

回答

0

你缺少雙引號在URL請嘗試以下

driver.get("http://steamcommunity.com/market/search?appid=730") 
+0

這只是一個副本並將錯誤粘貼到我的部分,它在我的原始代碼中有引號。 – daltonv297

2

使用用戶代理將解決ScriptException使用DesiredCapabilties這樣做,請參見下面的代碼:。

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); 
capabilities.setBrowserName("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20160101 Firefox/66.0"); 

HtmlUnitDriver driver = new HtmlUnitDriver(capabilities); 
driver.setJavascriptEnabled(true); 

driver.get("http://steamcommunity.com/market/search?appid=730"); 
System.out.println(driver.getPageSource()); 

這工作正常

雖然,替代解決方案將使用我發現在定位元素方面要好得多。

下載PhantomJS.exe文件,並設置該exe的文件路徑爲PHANTOMJS_EXECUTABLE_PATH_PROPERTY

public class CopyOfTest1 
{ 
PhantomJSDriver driver; 
WebDriverWait wait; 
DesiredCapabilities capabilities; 

CopyOfTest1() 
{ 
    //set binary path of phantomJS driver 
    capabilities = new DesiredCapabilities();  
    capabilities.setJavascriptEnabled(true); 
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "drivers/phantomjs.exe"); 
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX,"Y"); 
    capabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0"); 

    //intialize driver and set capabilties 
    driver = new PhantomJSDriver(capabilities); 
    //intitlaize webdriverwait class 
    wait = new WebDriverWait(driver, 30); 
} 

void start() 
{ 
    driver.get("http://steamcommunity.com/market/search?appid=730"); 
    System.out.println(driver.getPageSource()); 
    driver.quit(); 
} 

public static void main(String[] args) throws Exception 
{ 
    new CopyOfTest1().start(); 
} 
} 
+0

包含此代碼後,我仍然收到「異常調用fireEvent」錯誤。如果我嘗試使用不同的url,但是(http://steamcommunity.com/market/search?appid=730#p1_price_asc),我得到這個錯誤:'TypeError:無法從null(http:// steamcommunity -a.akamaihd.net/public/javascript/jquery-1.11.1.min.js?v=.isFTSRckeNhC#2)' – daltonv297

+0

此代碼適用於我。我建議你嘗試使用PhantomJSDriver。它更穩定。 – LittlePanda

+0

我嘗試過PhantomJSDriver,它給我的問題比以前更多!由於某種原因,這是完全不一致的,這意味着如果我多次運行程序而不更改代碼,每次都會得到不同的結果!即使使用'driver.quit()'或'driver.close()',phantomjs任務也不會退出,我必須使用任務管理器手動結束該過程。如果您知道解決此問題的任何方法,請告訴。 – daltonv297