Analice cada etiqueta li en el navegador usando '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 etiqueta.

I am able to do this with one link in li tag. I want to get all the li etiqueta debajoul <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>

preguntado el 04 de julio de 12 a las 07:07

3 Respuestas

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;
}

Respondido 04 Jul 12, 09:07

Its working fine if i open the new browser every time. If i am using the same browser it gives me error when the link gets clicked Error-"function evaluation timed out" at class_name. Do you know what is the cause regarding this - John

Are you using the watin page model? If so, you may have to re-grab your list page each time before you interrogate it. - phil lamberto

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

Respondido 04 Jul 12, 07:07

Prueba esto:

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

Respondido 04 Jul 12, 08:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.