Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open link from <a href> with HtmlUnit Java

I have an html file that has this part:

<td> <a href="/romarin/detail.do?ID=0"> NAME </a> </td>

How can I open this link on href with HtmlUnit?

My code:

 final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_11);
 final HtmlPage page1 = webClient.getPage("file:\\" + newrfile);
 final HtmlSubmitInput button = form.getInputByName("submit");
 final HtmlPage page2 = button.click();
 System.out.println(page2.asText());
 final HtmlForm form2 = page2.getFormByName("SearchForm");
like image 456
Kevin Rhema Akhwilla Avatar asked Oct 08 '15 08:10

Kevin Rhema Akhwilla


1 Answers

You can use getAnchorByHref:

HtmlAnchor htmlAnchor = page2.getAnchorByHref("/romarin/detail.do?ID=0");

Then you can click:

HtmlPage page3 = anchor.click();

Then you can save the page as a file:

page3.saveAs(some_file);

Or

System.out.println(page3.asXml());
like image 181
Ahmed Ashour Avatar answered Oct 12 '22 07:10

Ahmed Ashour