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

How to download the content of iframe using html unit where the ajax is used?

我要获取iframe的内容。 iframe 部分包含 table,其中一些信息需要通过 ajax 函数提取。我怎样才能得到他们的内容?提前致谢。

由于一些私人问题,非常抱歉,我只能提供URL的假link。

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']");

尝试使用 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());