次の html ファイル a.html を検討してください。
<html>
<frameset>
<frame src="frame_a.html">
</frameset>
</html>
frame_a.html は次のとおりです。
<html>
<body>
aaaaaa
</body>
</html>
次のコード:
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.WebClient;
import com.gargoylesoftware.htmlunit.html.FrameWindow;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class TestFramset {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
WebClient client = new WebClient(BrowserVersion.FIREFOX_17);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setRedirectEnabled(true);
client.getOptions().setThrowExceptionOnScriptError(true);
client.getOptions().setCssEnabled(true);
client.getOptions().setUseInsecureSSL(true);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
HtmlPage page = client.getPage("file:///...a.html");
System.out.println("page as text will give nothing:"+page.asText());
System.out.println("recursive function will give:"+getText(page));
}
public static String getText (HtmlPage page) {
String text = page.asText();
List<FrameWindow> frames = page.getFrames();
for (FrameWindow frame:frames) {
text += getText((HtmlPage) frame.getEnclosedPage());
}
return text;
}
}
出力が得られます
テキストとしてのページは何も与えません:
再帰関数は次のようになります:aaaaaa
私の質問は、page.asText 関数がフレームのテキストを返さないという事実が望ましいかどうか、およびフレームのテキストを再帰的に取得する方法が最善の方法であるかどうかです。