Reputation: 11
I try to make a program and I need to click a button using my program
webBrowser1.Document.GetElementById("submit").InvokeMember("click");
This doesn't work because this button has no id.
Here is the code of the button:
<a class="buttonMtel" tabindex="6" onclick="document.forms['SMSForm'].submit()">
<span>Изпрати</span>
</a>
<span>Изпрати</span>.
Please help. Thank you.
Upvotes: 1
Views: 1493
Reputation: 1038710
Assuming that your button is the only anchor with class="buttonMtel"
, you could use the GetElementByTagName
method and then locate the desired element:
var anchors = webBrowser1.Document.GetElementByTagName("a");
foreach (HtmlElement anchor in anchors)
{
if (anchor.GetAttribute("class") == "buttonMtel")
{
// we have found the button => click on it
anchor.InvokeMember("click");
break;
}
}
or if you prefer LINQ:
var anchor = webBrowser1
.Document
.GetElementByTagName("a")
.Cast<HtmlElement>()
.FirstOrDefault(anchor => anchor.GetAttribute("class") == "buttonMtel");
if (anchor != null)
{
anchor.InvokeMember("click");
}
If on the other hand you have multiple anchors with class="buttonMtel"
in your DOM you will need a different approach. Depending on the actual structure of your DOM you might need to first locate some parent element and then reach up to the desired button.
Upvotes: 1