Search code examples
hyperlinkbrowserwatin

how i click on any links i want in watin


i use this code for find links in watin web browser

var lnk = browser.links.where(x => x.url.contains("example text"));

i accessed to founded links for click only first and last link but i want also click on any links example click on average link. i can get count of links with count() code and /2 for get average number of link and click on it.

so if i have 4 links i want click on second link and when i have 2 links click on 1st link

how i can do this?


Solution

  • So you just want to click on the n/2th link?

    If you make lnk into an indexable list, that should be pretty easy. First make it a list:

    var lnk = browser.Links.Where(x => x.Url.Contains("example text")).ToList();
    

    Then you can reference elements by their index:

    lnk[lnk.Count / 2].Click();
    

    Conversely, you can use something like .Skip() in any enumeration so you don't have to incur the overhead of making it a list. Something like this:

    var lnk = browser.Links.Where(x => x.Url.Contains("example text"));
    lnk.Skip(lnk.Count() / 2).First().Click();
    

    There are many methods available for enumerations which allow you to seek, transform, manipulate, and do just about anything you need to with the enumeration.