我将获得iframe的内容。 iframe部分包含表,其中一些信息需要通过ajax函数提取。我怎样才能得到它们的内容?提前谢谢。
根据一些私人问题,我很抱歉,我只能提供URL的虚假链接。
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
//creat a new WebClient object which is equal to browser
URL url= new URL("http://www.yahoo.com/");
HtmlPage page=(HtmlPage) webClient.getPage(url);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.SEVERE);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
final HtmlPage startPage = webClient.getPage("http://www.yahoo.com/");
String text = startPage.asText();
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
List<HtmlInlineFrame> anchors4 = (List<HtmlInlineFrame>) startPage.<HtmlInlineFrame>getByXPath("//iframe[@id='contentFrame']");
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
System.out.println(anchors4.get(0).getTextContent());
List<HtmlDivision> anchors2 = (List<HtmlDivision>) startPage.<HtmlDivision>getByXPath("//div[@class='login_button']");
anchors2.get(0).click();
List<HtmlInlineFrame> anchors3 = (List<HtmlInlineFrame>) startPage.<HtmlInlineFrame>getByXPath("//iframe[@id='contentFrame']");
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
System.out.println("this" + anchors3.get(0).asXml());
List<HtmlTable> htmlTable = (List<HtmlTable>) startPage.<HtmlTable>getByXPath("//table[@class='Result']");
答案 0 :(得分:1)
尝试使用FrameWindow
(他的,你需要你的iframe
拥有name
属性):
// ...
startPage = anchors2.get(0).click();
HtmlPage framePage = (HtmlPage) startPage.getFrameByName("your_frame_name").getEnclosedPage();
System.out.println("The content that you need: " + framePage.asXml());