我想使用HtmlUnit获取facebook帖子的xpath。您可以参考这两个问题我们就可以得到我想要做更多的想法:使用HtmlUnit获取Facebook页面帖子的XPath
为了模仿我做什么,你可以按照Q-1。 HTML代码(facebook页面)的pastebin链接是http://pastebin.com/MfXsYSJQ。
或者只是你可以去https://www.facebook.com/bhramakarserver。 我只想获得包含帖子文本的span的xpath:“嗨!这是此页面的第一篇文章。” 我试过是这样的:
public class ForStackOverflow {
public static void main(String[] args) throws IOException {
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);
client.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page1=client.getPage("https://www.facebook.com/bhramakarserver");
System.out.println(page1.asXml());
//getting the xpath of span of class="userContent"
HtmlInput input=(HtmlInput)page1.getByXPath("/html/body//input[@type='submit']").get(0);
System.out.println(input.asXml());
//This line gives error as the xpath evaluates to null
HtmlSpan span=(HtmlSpan)page1.getByXPath("/html/body//span[@class='userContent']").get(0);
}
}
,这似乎是在第1页有静态html的问题。在此,span元素:
<span data-ft="{"tn":"K"}" class="userContent">Hi! this is the first post of this page.</span>
是动态生成的。因此,它看起来像在page1的html中注释,但通过检查元素进行检查时,它显示为正常。因此,它的动态uncommented.Is没有办法,我可以让page1的html在所有的动态内容已被加载后处于状态,以便我可以正确地获得xpath?可以使用硒驱动程序完成吗?
@MostyMostacho可以帮忙吗? – rahulserver