为什么 HtmlUnit 代码与 Chrome 代码不匹配?
Why HtmlUnit code doesn't match Chrome code?
我正在尝试抓取网络路由器页面以搜索连接的设备和有关它们的信息。
我写了这段代码:
String searchUrl="http://192.168.1.1";
HtmlPage page=client.getPage(searchUrl);
System.out.println(page.asXml());
问题是 HtmlUnit 的代码 returns 与 Chrome 中的代码不同。在 HtmlUnit 中,我没有列出已连接设备的代码部分。
尝试这样的事情
try (final WebClient webClient = new WebClient()) {
// do not stop at js errors
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getPage(searchUrl);
// now wait for the js starting async (you can play with the timing)
webClient.waitForBackgroundJavaScript(10000);
// maybe the page was replaced from the async js
HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println(page.asXml());
}
通常这会有帮助。
如果你仍然遇到问题,你必须在 github (https://github.com/HtmlUnit/htmlunit) 上打开一个问题。
但请记住,如果我可以 run/and 调试您的代码,我才能真正提供帮助 - 意味着您的 Web 应用程序必须 public。
我正在尝试抓取网络路由器页面以搜索连接的设备和有关它们的信息。 我写了这段代码:
String searchUrl="http://192.168.1.1";
HtmlPage page=client.getPage(searchUrl);
System.out.println(page.asXml());
问题是 HtmlUnit 的代码 returns 与 Chrome 中的代码不同。在 HtmlUnit 中,我没有列出已连接设备的代码部分。
尝试这样的事情
try (final WebClient webClient = new WebClient()) {
// do not stop at js errors
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getPage(searchUrl);
// now wait for the js starting async (you can play with the timing)
webClient.waitForBackgroundJavaScript(10000);
// maybe the page was replaced from the async js
HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println(page.asXml());
}
通常这会有帮助。 如果你仍然遇到问题,你必须在 github (https://github.com/HtmlUnit/htmlunit) 上打开一个问题。
但请记住,如果我可以 run/and 调试您的代码,我才能真正提供帮助 - 意味着您的 Web 应用程序必须 public。