I have a pa开发者_Python百科ge in my C# webbrowser that contains
<input type="submit" value="Sign out">
Since it does not have an id, I am unable to use the webbrowser's htmldocument to get it by an id and invoke its click. How would I click it using htmldocument now?
You can give it your own ID you know, it's doesn't have to be a server control for that. Just do:
<input type="submit" ID="MySubmitButton1" value="Sign out">
You mention you want to call something when it is clicked, alternatively just add the onclick event manually:
<input type="submit" onclick="CallMyFunction()" value="Sign out">
Pass 'this' into it if you want to pass the input control into the function:
<input type="submit" onclick="CallMyFunction(this)" value="Sign out">
You probably don't want the submit button to post anything when you call your function as well, so you probably want something like:
<input type="submit" onclick="return CallMyFunction()" value="Sign out">
function CallMyFunction()
{
bool IsFormValid = false;
// Check if form is valid
return IsFormValid;
}
Assuming you don't have control over the HTML page and therefore cannot add an ID to the element, you could use the HtmlDocument.GetElementsByTagName
to get all input elements, and then filter that collection by the type
and value
attributes. Something like:
var firstMatchingSubmit = (from input in myDocument.GetElementsByTagName("input")
where input.GetAttribute("type") == "submit" &&
input.GetAttribute("value") == "Sign out"
select input).FirstOrDefault();
if (firstMatchingSubmit != null)
{
firstMatchingSubmit.RaiseEvent("click");
}
Note that this approach is not appropriate if there are multiple matching elements (only the first one will be clicked).
What about this:
webbrowser1.Document.Forms[0].InvokeMember("submit");
How to handle page with multiple html submit button with no id? How to identify the submit button from 2nd row.Multiple html buttons
精彩评论