如何以编程方式单击ASP.NET网页上的按钮?

时间:2021-10-09 03:17:54

I am trying to figure out how to click a button on a web page programmatically.

我试图弄清楚如何以编程方式单击网页上的按钮。

Specifically, I have a WinForm with a WebBrowser control. Once it navigates to the target ASP.NET login page I'm trying to work with, in the DocumentCompleted event handler I have the following coded:

具体来说,我有一个带WebBrowser控件的WinForm。一旦它导航到我正在尝试使用的目标ASP.NET登录页面,在DocumentCompleted事件处理程序中,我有以下编码:

HtmlDocument doc = webBrowser1.Document;

HtmlElement userID = doc.GetElementById("userIDTextBox");
userID.InnerText = "user1";

HtmlElement password = doc.GetElementById("userPasswordTextBox");
password.InnerText = "password";

HtmlElement button = doc.GetElementById("logonButton");
button.RaiseEvent("onclick");

This fills the userid and password text boxes fine, but I am not having any success getting that darned button to click; I've also tried "click", "Click", and "onClick" -- what else is there?. A search of msdn of course gives me no clues, nor groups.google.com. I gotta be close. Or maybe not -- somebody told me I should call the POST method of the page, but how this is done was not part of the advice given.

这样可以很好地填充用户标识和密码文本框,但是我没有成功获得点击的按钮;我也试过“点击”,“点击”和“onClick” - 还有什么?搜索msdn当然不会给我提供任何线索,也不会提供groups.google.com。我得亲近或许不是 - 有人告诉我应该调用页面的POST方法,但是这样做是不是给出了建议的一部分。

BTW The button is coded:

BTW按钮编码:

<input type="submit" name="logonButton" value="Login" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="logonButton" tabindex="4" />

9 个解决方案

#1


8  

How does this work? Works for me

这个怎么用?适合我

HtmlDocument doc = webBrowser1.Document;

doc.All["userIDTextBox"].SetAttribute("Value", "user1");
doc.All["userPasswordTextBox"].SetAttribute("Value", "Password!");
doc.All["logonButton"].InvokeMember("Click");

#2


6  

var btn = document.getElementById(btnName); if (btn) btn.click();

var btn = document.getElementById(btnName); if(btn)btn.click();

#3


2  

There is an example of how to submit the form using InvokeMember here. http://msdn.microsoft.com/en-us/library/ms171716.aspx

这里有一个如何使用InvokeMember提交表单的示例。 http://msdn.microsoft.com/en-us/library/ms171716.aspx

#4


1  

You can try and invoke the Page_ClientValidate() method directly through the clientscript instead of clicking the button, let me dig up an example.

您可以尝试直接通过clientscript调用Page_ClientValidate()方法,而不是单击按钮,让我挖掘一个示例。

Using MSHTML

mshtml.IHTMLWindow2 myBroserWindow = (mshtml.IHTMLWindow2)MyWebBrowser.Document.Window.DomWindow;
myBroserWindow.execScript("Page_ClientValidate();", "javascript");

#5


1  

Have you tried fireEvent instead of RaiseEvent?

你尝试过fireEvent而不是RaiseEvent吗?

#6


1  

You could call the method directly and pass in generic object and EventArgs parameters. Of course, this might not work if you were looking at the sender and EventArgs parameters for specific data. How I usually handle this is to refactor the guts of the method to a doSomeAction() method and the event handler for the button click will simply call this function. That way I don't have to figure out how to invoke what is usually just an event handler to do some bit of logic on the page/form.

您可以直接调用该方法并传入泛型对象和EventArgs参数。当然,如果您查看特定数据的sender和EventArgs参数,这可能不起作用。我通常如何处理这个方法是将方法的内容重构为doSomeAction()方法,按钮单击的事件处理程序将简单地调用此函数。这样我就不必弄清楚如何调用通常只是一个事件处理程序来在页面/表单上做一些逻辑。

In the case of javascript clicking a button for a form post, you can invoke form.submit() in the client side script -- which will run any validation scripts you defined in the tag -- and then parse the Form_Load event and grab the text value of the submit button on that form (assuming there is only one) -- at least that's the ASP.NET 1.1 way with which I'm very familiar... anyone know of something more elegant with 2.0+?

在javascript单击表单帖子的按钮的情况下,您可以在客户端脚本中调用form.submit() - 它将运行您在标记中定义的任何验证脚本 - 然后解析Form_Load事件并获取该表单上的提交按钮的文本值(假设只有一个) - 至少这是我非常熟悉的ASP.NET 1.1方式...任何人都知道2.0+更优雅的东西?

#7


1  

Just a possible useful extra where the submit button has not been given an Id - as is frequently the case.

只是一个可能有用的额外的地方,提交按钮没有给出一个Id - 通常情况下。

    private HtmlElement GetInputElement(string name, HtmlDocument doc) {
            HtmlElementCollection elems = doc.GetElementsByTagName("input");

            foreach (HtmlElement elem in elems)
            {
                String nameStr = elem.GetAttribute("value");
                if (!String.IsNullOrEmpty (nameStr) && nameStr.Equals (name))
                {
                    return elem;
                }
            }
            return null;
    }

So you can call it like so:

所以你可以像这样调用它:

 GetInputElement("Login", webBrowser1.Document).InvokeMember("Click");

It'll raise an exception if the submit input with the value 'Login', but you can break it up if you want to conditionally check before invoking the click.

如果提交输入值为“Login”,则会引发异常,但如果要在调用单击之前有条件地检查,则可以将其分解。

#8


0  

You posted a comment along the lines of not wanting to use a client side script on @Phunchak's answer. I think what you are trying to do is impossible. The only way to interact with the form is via a client side script. The C# code can only control what happens before the page is sent out to the browser.

您发布了一条评论,不希望在@ Phunchak的回答中使用客户端脚本。我认为你要做的事情是不可能的。与表单交互的唯一方法是通过客户端脚本。 C#代码只能控制页面发送到浏览器之前发生的事情。

#9


0  

try this button.focus System.Windows.Forms.SendKeys.Send("{ENTER}")

试试这个button.focus System.Windows.Forms.SendKeys.Send(“{ENTER}”)

#1


8  

How does this work? Works for me

这个怎么用?适合我

HtmlDocument doc = webBrowser1.Document;

doc.All["userIDTextBox"].SetAttribute("Value", "user1");
doc.All["userPasswordTextBox"].SetAttribute("Value", "Password!");
doc.All["logonButton"].InvokeMember("Click");

#2


6  

var btn = document.getElementById(btnName); if (btn) btn.click();

var btn = document.getElementById(btnName); if(btn)btn.click();

#3


2  

There is an example of how to submit the form using InvokeMember here. http://msdn.microsoft.com/en-us/library/ms171716.aspx

这里有一个如何使用InvokeMember提交表单的示例。 http://msdn.microsoft.com/en-us/library/ms171716.aspx

#4


1  

You can try and invoke the Page_ClientValidate() method directly through the clientscript instead of clicking the button, let me dig up an example.

您可以尝试直接通过clientscript调用Page_ClientValidate()方法,而不是单击按钮,让我挖掘一个示例。

Using MSHTML

mshtml.IHTMLWindow2 myBroserWindow = (mshtml.IHTMLWindow2)MyWebBrowser.Document.Window.DomWindow;
myBroserWindow.execScript("Page_ClientValidate();", "javascript");

#5


1  

Have you tried fireEvent instead of RaiseEvent?

你尝试过fireEvent而不是RaiseEvent吗?

#6


1  

You could call the method directly and pass in generic object and EventArgs parameters. Of course, this might not work if you were looking at the sender and EventArgs parameters for specific data. How I usually handle this is to refactor the guts of the method to a doSomeAction() method and the event handler for the button click will simply call this function. That way I don't have to figure out how to invoke what is usually just an event handler to do some bit of logic on the page/form.

您可以直接调用该方法并传入泛型对象和EventArgs参数。当然,如果您查看特定数据的sender和EventArgs参数,这可能不起作用。我通常如何处理这个方法是将方法的内容重构为doSomeAction()方法,按钮单击的事件处理程序将简单地调用此函数。这样我就不必弄清楚如何调用通常只是一个事件处理程序来在页面/表单上做一些逻辑。

In the case of javascript clicking a button for a form post, you can invoke form.submit() in the client side script -- which will run any validation scripts you defined in the tag -- and then parse the Form_Load event and grab the text value of the submit button on that form (assuming there is only one) -- at least that's the ASP.NET 1.1 way with which I'm very familiar... anyone know of something more elegant with 2.0+?

在javascript单击表单帖子的按钮的情况下,您可以在客户端脚本中调用form.submit() - 它将运行您在标记中定义的任何验证脚本 - 然后解析Form_Load事件并获取该表单上的提交按钮的文本值(假设只有一个) - 至少这是我非常熟悉的ASP.NET 1.1方式...任何人都知道2.0+更优雅的东西?

#7


1  

Just a possible useful extra where the submit button has not been given an Id - as is frequently the case.

只是一个可能有用的额外的地方,提交按钮没有给出一个Id - 通常情况下。

    private HtmlElement GetInputElement(string name, HtmlDocument doc) {
            HtmlElementCollection elems = doc.GetElementsByTagName("input");

            foreach (HtmlElement elem in elems)
            {
                String nameStr = elem.GetAttribute("value");
                if (!String.IsNullOrEmpty (nameStr) && nameStr.Equals (name))
                {
                    return elem;
                }
            }
            return null;
    }

So you can call it like so:

所以你可以像这样调用它:

 GetInputElement("Login", webBrowser1.Document).InvokeMember("Click");

It'll raise an exception if the submit input with the value 'Login', but you can break it up if you want to conditionally check before invoking the click.

如果提交输入值为“Login”,则会引发异常,但如果要在调用单击之前有条件地检查,则可以将其分解。

#8


0  

You posted a comment along the lines of not wanting to use a client side script on @Phunchak's answer. I think what you are trying to do is impossible. The only way to interact with the form is via a client side script. The C# code can only control what happens before the page is sent out to the browser.

您发布了一条评论,不希望在@ Phunchak的回答中使用客户端脚本。我认为你要做的事情是不可能的。与表单交互的唯一方法是通过客户端脚本。 C#代码只能控制页面发送到浏览器之前发生的事情。

#9


0  

try this button.focus System.Windows.Forms.SendKeys.Send("{ENTER}")

试试这个button.focus System.Windows.Forms.SendKeys.Send(“{ENTER}”)