Click to a random url on page with GeckoFX

700 views Asked by At

I am trying to randomly Click/Navigate to a URL in my browser after the page is finished loading. I'm fairly new to C# and GeckoFX, I'm guessing the steps should be;

  • Collect urls from the loaded page
  • Check if they are in href and starting with http
  • Navigate to a random selection from the result

So far I can get the urls but I dont know what to do next;

    private void LoadingFinished(object sender, EventArgs args)
    {
            foreach (GeckoElement htmlElement in geckoWebBrowser1.Document.Links)
            {
                string linkItem = htmlElement.GetAttribute("href").ToString();

            }
    }
1

There are 1 answers

4
Iwan1993 On BEST ANSWER

You could try the following

var links = new List<GeckoElement>()
foreach(var link in browser.Document.Links) {
   if(!String.IsNullOrEmpty(link.GetAttribute("href").ToString()))
      links.Add(link);
}
if(links.Count > 0)
   ((GeckoHtmlElement)links[new Random().Next(0, links.Count)]).Click()
else
   MessageBox.Show("No Links found")