Speci
Speci

Reputation: 374

How to Invoke a Click if the element don't have an ID?

I want to Invoke a Click in an Element that don't have an ID. I have tried this:

webBrowser1.Document.GetElementById("element>a").InvokeMember("Click");

So i have used the selector but it is not working. If i can't Invoke a Click in an Element without ID then can i Invoke a Click in an Element just by using the Inner Text? Like this : webBrowser1.Document.GetElementByInnerText or something like this where you give the text and it find the element by looking for the text. Thanks for helping!

Upvotes: 0

Views: 52

Answers (1)

Amr Elgarhy
Amr Elgarhy

Reputation: 68902

You will need to loop through the anchor elements and check for parent foreach if it has the classname you want:

foreach (HtmlElement elem in webBrowser1.Document.GetElementsByTagName("a"))
{
    if (elem.Parent.GetAttribute("className") == "element")
    {
       elem.InvokeMember("Click");
    }
}

You can also check for InnerText while looping.

References:
https://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument(v=vs.110).aspx

Upvotes: 1

Related Questions