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