Tearsdontfalls Tearsdontfalls - 1 year ago 292
C# Question

GeckoFX: Cast GeckoNode to GeckoHTMLElement

I simply need to do a Click on the first Child of an Element identified by a unique Class.

My Code so far is:


The Problem is that GeckoFX 16's
returns an Array of the Type
, which doesnt have the Click Method, so this Code wouldnt work. On the other side, the Method


returns an Array of the Type
, which does have a Click Method.

Based on that fact, I tried casting with the
Operator, but this always throws an Exception with the message that it cant be casted.
I searched already alot in GeckoFX's Classes(sadly there isnt any documentation), but I dont found anything

Any help will be greatly appreciated.

Tom Tom
Answer Source

Before casting you need to ensure the node is actually an element, and not, for example, a text node.

GeckoNode node = geckoWebBrowser.Document.GetElementsByClassName("button")[0].FirstChild;
if (NodeType.Element == node.NodeType)
  GeckoElement element = (GeckoElement)node;
 // Even though GetElementByClassName return type could contain non elements, I don't think
 // it ever would in reality.
 Console.WriteLine("First node is a {0} not an element.", node.NodeType);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download