目前,我試圖用htmlunit控制谷歌地圖JP。 這裏是谷歌地圖JP的a link。 在我的瀏覽器中,左側有一個菜單(舊風格視圖),並且在我點擊縣名稱後顯示城市名稱。我想通過使用htmlunit執行相同的操作,但沒有任何更改。 請給我一些想法和幫助(使用servlet atm)。不能點擊鏈接谷歌地圖頁面與htmlunit
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
}
}
Here's a image file for explanation!
歡迎任何建議。 在此先感謝。
編輯
還在尋找解決方案
確保在適當的錨點列表中的元素12想要單擊? – Kick
感謝您的評論。是的,我非常確定元素12是正確的。它說「北海道」,與System.out.println(links.get(12).asText())。如果您有任何想法,請幫助我... – user3348619