HtmlUnit and JavaScript in links

2020-04-19 05:37发布

Copied from here:

I need to save files from websites using HtmlUnit. I am currently navigating to pages that have several anchors that use javascript:

 onClick()="DownloadAttachment('attachmentId')" 

So far though I've been unable to find resources or examples that show how to save files using HtmlUnit. I've been trying mainly to get AttachmentHandler to work for this as it seems the most likely to work, but have been unsuccessful.

How do I use AttachmentHandler to get at the data stream which the browser would see if I clicked on the link?

1条回答
萌系小妹纸
2楼-- · 2020-04-19 05:54

It's more simple than that. Just simulate the click event and then get the WebResponse:

HtmlElement anchorAttachment = (HtmlElement) pageETrackerItem.getByXPath(".../a").get(0);

InputStream is = anchorAttachment.click().getWebResponse().getContentAsStream();
查看更多
登录 后发表回答