How to find div inside another div using HtmlUnit?

I am working on some project where I need to pick up some information from another site. I am using HtmlUnit for this purpose, but the problem is that I cannot go through the elements on the same page.

Example:

  <div id="some_id"> <div> <div> <div> ...... many divs in between ...... <div id="my_target_div"> some information </div> ........ ........ </div> 

Now, how to get div with id my_target_div and information inside div

+6
source share
2 answers

Use getHtmlElementById .

Mark the documentation .

Example:

 @Test public void getElements() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://some_url"); final HtmlDivision div = page.getHtmlElementById("my_target_div"); webClient.closeAllWindows(); } 

A source

+5
source
 WebClient webClient = new WebClient(); HtmlPage page; HtmlElement div= (HtmlElement) page2.getFirstByXPath("//div[@id='my_target_div']"); 

This will solve your problem.

+2
source

Source: https://habr.com/ru/post/947836/


All Articles