目前,我正试图用htmlunit控制谷歌地图JP。 这是Google地图JP的a link。 在我的浏览器中,左侧有一个菜单(旧式视图),它在我点击县名后显示城市名称。我想通过使用htmlunit做同样的动作,但没有任何变化。 请给我一些想法并帮助我(使用servlet atm)。
package com.example.part1;
import java.util.List;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
@WebServlet(“/test")
public class test extends HttpServlet {
private static final long serialVersionUID = 1L;
public test() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
response.setContentType("text/html; charset=Windows-31J");
final WebClient webClient = new WebClient(BrowserVersion.CHROME, "198.23.143.27", 5555);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page1 = webClient.getPage("https://maps.google.co.jp/");
List<HtmlAnchor> links = (List<HtmlAnchor>) page1.getByXPath("//a[@href='javascript:void(0)']");
//click 北海道
links.get(12).click();
webClient.waitForBackgroundJavaScript(10000);
response.getWriter().write(page1.getWebResponse().getContentAsString());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
这是a image file for explanation!
欢迎任何建议。 在此先感谢。
修改
仍在寻找解决方案
答案 0 :(得分:0)
单击anchor
时,将结果保存在HtmlPage中,如下所示:
page1 = links.get(12).click();
Edit.Added类点击按钮,还添加了点击
后收到的页面截图import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class Test {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.waitForBackgroundJavaScript(10000);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = webClient.getPage("https://maps.google.co.jp/");
List<HtmlAnchor> links = (List<HtmlAnchor>) page.getByXPath("//a[@href='javascript:void(0)']");
page = links.get(12).click();
FileWriter writer = new FileWriter("d:\\test.html"); //To write page in file
writer.write(page.asXml());
}
}