John
John

Reputation: 115

Parse through Each li tag in browser using 'WatiN'

I am using a watin dll to browse through a webpage, click on a link in li tag, go to the next page, fetch some data, go back to previous page and click the link in the next li tag.

I am able to do this with one link in li tag. I want to get all the li tag underul <classname> click on each link and perform the above procedure. How can I get all the li and loop through each page?

HTML code of the page is like this:

<ul id="ul_classname" class="search-result-set">
    <li class="">
        <div class="Div_Classname">
            <h3 class="standard_font">
                <a class="a class_name" href="link to be clicked">text to be displayed</a>
            </h3>
            <p class="word-wrap"></p>
        </div>
    </li>
    <li class="">
        <div class="Div_Classname">
            <h3 class="standard_font">
                <a class="a class_name" href="link to be clicked">text to be displayed</a>
            </h3>
            <p class="word-wrap"></p>
        </div>
    </li>
</ul>

Upvotes: 0

Views: 2109

Answers (3)

Phil Lambert
Phil Lambert

Reputation: 1047

HTH!

private void CrawlSite()
{
    int idx = 0;
    do
    {
        idx = this.ClickLink(idx);
    }
    while (idx != -1);
}

private int ClickLink(int idx)
{
    WatiN.Core.Browser browser = GetBrowser();

    ListItemCollection listItems = browser.List("ul_classname").ListItems;
    if (idx > listItems.Count - 1)
        return -1;

    Link lnk = listItems[idx].Link(Find.ByClass("a class_name"));
    lnk.Click();

    //TODO: get your data

    browser.Back();

    return idx + 1;
}

Upvotes: 1

t3hn00b
t3hn00b

Reputation: 912

Try this:

LinkCollection links = ie.Links;
foreach (var link in links)
{
    link.Click();
    // Do something
    ie.Back();
}

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28990

you can try with this code (Linq to XML)

var xdoc = XDocument.Load(yourFile);

var terms= from term in xdoc.Descendants("ul")
        select new
        {
              Class= term.Attribute("class").Value
        };

foreach(var li in terms)
{
   Console.Write(li.Class);
}

Upvotes: 1

Related Questions