driver.get无法在Selenium Webdriver,Java中工作

时间:2015-05-09 23:50:56

标签: java javascript selenium selenium-webdriver steam

在某个网站(driver.get("http://steamcommunity.com/market/search?appid=730"))上使用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 时,我收到此错误:

true

它可以与其他网址一起使用,并且它也可以使用此功能,直到我通过将WebDriver driver = new HtmlUnitDriver(true);放入HiddenDiv来启用javascript。我需要这样做是因为我的程序在没有javascript的情况下没有正常工作,并且看起来这个Steam社区市场页面需要javascript才能访问它,即使只是输入url。我做错了什么?

2 个答案:

答案 0 :(得分: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());

这很好用。

虽然,另一种解决方案是使用PhantomJSDriver,我发现它在定位元素方面要好得多。

下载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();
}
}

答案 1 :(得分:0)

您在网址中缺少双引号。请尝试以下

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