如何使用使用ajax的html单元下载iframe的内容?

时间:2016-03-16 06:03:27

标签: iframe htmlunit

我将获得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']");

1 个答案:

答案 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());