【问题标题】:How to handle javascript error in phantomjs selenium with java?如何用java处理phantomjs selenium中的javascript错误?
【发布时间】:2018-01-16 21:07:11
【问题描述】:

我尝试了很多,但我没有得到任何解决方案如何处理 page.onError ,代码如下 -

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setJavascriptEnabled(true);
desiredCapabilities.setCapability("takesScreenshot", false);
WebDriver driver = new PhantomJSDriver(desiredCapabilities);

我收到以下错误 -

[错误 - 2017-08-09T06:00:16.732Z] 会话 [db2e4bf0-7cc7-11e7-a9b1-81e10abf02db] - page.onError - 消息: TypeError:null 不是对象(评估 'document.getElementById("img").src=images[x]')

phantomjs://platform/console++.js:263 错误 [错误 - 2017-08-09T06:00:16.733Z] 会话 [db2e4bf0-7cc7-11e7-a9b1-81e10abf02db] - page.onError - 堆栈:

【问题讨论】:

  • 错误Session [db2e4bf0-7cc7-11e7-a9b1-81e10abf02db] - page.onError - msg: TypeError: null is not an object (evaluating 'document.getElementById("img").src=images[x]') 说明一切 PhantomJS 似乎无法启动会话。
  • 错误何时发生以及您想要做什么?您的代码似乎与引发异常的代码不同
  • @TarunLalwani 我正在抓取一些网站,但一段时间后我收到此错误
  • 查看错误似乎document.getElementById("img") 变为空或images 对象为空。这可能是由于不同的事情、页面加载错误、页面上的不同 html 造成的。刮擦保护改变页面
  • @TarunLalwani 如何处理 selenium java 中的页面加载错误,我想如果 javascript 中有一些错误,它应该 scape 并继续,但我不知道该怎么做。跨度>

标签: java selenium selenium-webdriver phantomjs


【解决方案1】:

要使用Selenium 3.4.0PhantomJS 2.1.1,您需要提供PhantomJS binarySystem.setProperty 命令的绝对路径,如下所示:

File src = new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setJavascriptEnabled(true);
desiredCapabilities.setCapability("takesScreenshot", false);
WebDriver driver = new PhantomJSDriver();
driver.get("https://www.facebook.com/");
System.out.println("Facebook Page Loaded");

【讨论】:

  • 让我试试看。
猜你喜欢
  • 1970-01-01
  • 2015-09-28
  • 2018-10-05
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
相关资源
最近更新 更多