Neeeck
Neeeck

Reputation: 68

How to get the index value from html element?

I have a sample html :

<div class="linkWrap">
    <a href="http://google.com">Google</a>
    <a href="http://stackoverflow.com">Stackoverflow</a>
    <a href="http://wikipedia.org">Wikipedia</a>
</div>

I want to get index value of html element.

How do I get index value of html element? like jQuery's .index()

public int GetLinkIndex(string url)
{
    // return browser.Link(Find.ByUrl(url)).Index()
}

Thanks for reading.


I've tried. But look a little wrong..

public int GetLinkIndex(string url)
{
    if(browser.Link(Find.ByUrl(url)).Exists)
    {
        int i = 0;
        while(true)
        {
            string href = browser.Link(Find.BySelector(".linkWrap > a") && Find.ByIndex(i)).GetAttributeValue("href");
            if(href == url)
            {
                return i;
            }
            i += 1;
        }
    }
}

Upvotes: 0

Views: 1485

Answers (2)

Sham
Sham

Reputation: 840

Is it for all the elements or only for specific elements type. As you mentioned in the sample code, it seems to be for Links. Then the following code would help you

public Int GetListIndex(string url)
{
   LinkCollection links = browser.Links;
   int count = -1;
   foreach(Link lnk in links)
   {
      if(lnk.GetAttributeValue("href").Contains(url))
      {
         return ++count;
      }
     count++;
    }
 }

Upvotes: 1

chris
chris

Reputation: 57

You could try to safe your html-file in an String-Array with this Stream reader function msdn.microsoft.com/de-de/library/aa287535(v=vs.71).aspx

than you can deleate the first and the last row, with contains "" and ""

next you can use the trim Funktion to deleate the first 8 and the last 4 characters in every line. msdn.microsoft.com/de-de/library/d4tt83f9(v=vs.110).aspx

now you have an Array with entrys like ""http://google.com">Google". you can use the split-funktion to split the http-part, from the Name msdn.microsoft.com/de-de/library/b873y76a(v=vs.110).aspx

store both parts in an 2-dimensional array. now you have an indexed array of your URLs

Upvotes: 0

Related Questions