Search code examples
c#geckofx

How to find and click links/buttons by href? (GeckoFx browser)


In the past I were using

       GeckoElementCollection elements = iframe.ContentDocument.GetElementsByName("name");
            foreach (var element in elements)
            {

                 if (element.GetAttribute("href") == "text")
                 {
                     MessageBox.Show(element.GetAttribute("class"));
                 }
              }

So I were finding element by name and then I checked href if its element which im trying to find. Now I dont have any element name only class (class is same for few elements) and what is unique is href. So I need to search element by href and perform click/input events with it.

I believe that I need to use GeckoNode

    GeckoNode node = iframe.ContentDocument.GetElementsByClassName("classname")[0].FirstChild;
             if (NodeType.Element == node.NodeType)
             {
                 MessageBox.Show(node.GetAttribute("hreff"));

             }

However GetAttribute dont work with nodes.. Any ideas?


Solution

  • I know the question is old, but today I had same problem and I fix it. Try something like this :

    GeckoElementCollection fblikes = webBrowser.Document.GetElementsByTagName("a");
                foreach (GeckoHtmlElement item in fblikes)
                {
                    string aux = item.GetAttribute("href");
                    if (!string.IsNullOrWhiteSpace(aux) && aux.Equals("p.php?p=facebook"))
                    {
                        item.Click();
    
                    }
                }