How to find div inside another div using HtmlUnit?
Asked Answered
L

2

7

I am working on some project where in i need scrap some information from different website.I am using HtmlUnit for this purpose,But problem is i am unable to traverse through the elements on one page.

Example:

  <div id="some_id">

      <div>

        <div>

           <div>

              ......
                       many divs in between
              ......

               <div id="my_target_div"> some information </div>

                ........

                ........

                 </div>

Now how get div with id my_target_div and information inside that div

Launch answered 21/6, 2013 at 12:48 Comment(1)
what have you already tried?Scurlock
S
5

Use getHtmlElementById.

Check documentation.

An 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();
}

Source.

Scurlock answered 7/8, 2013 at 12:18 Comment(1)
Thanks for the answer but, i have already solved it myself. Anyway i am up voting you for spending your quality time in answering this question, which may help others who have similar problem.Launch
V
2
WebClient webClient = new WebClient();
        HtmlPage page;
  HtmlElement div= (HtmlElement) page2.getFirstByXPath("//div[@id='my_target_div']");

This will solve your problem.

Vaca answered 16/11, 2016 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.