[英]C# How to Click Button automatically via WebBrowser
The Html code of my click page is:我的点击页面的Html代码是:
<input type="submit" id="publishButton-ns" class="ubtn ubtn-block"
name="publish" tabindex="10" value="Publish Post">
I tried this code for clicking:我试过这个代码来点击:
webBrowser1.Document.GetElementById("publishButton-ns").InvokeMember("click");
but this not found the button.但这没有找到按钮。
This may help you.这可能会对您有所帮助。
<input type="submit" value="Submit" />
HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement el in elc)
{
if (el.GetAttribute("type").Equals("submit"))
{
el.InvokeMember("Click");
}
}
Are you waiting for the page to load first?您是否在等待页面首先加载? You should bind a function in your code to wait for the page to load, them click the button:
您应该在代码中绑定一个 function 以等待页面加载,然后单击按钮:
static void form1_Load() {
// ...
webBrowser1.onDocumentReady += webBrowser_DocumentReady;
}
static void webBrowser1_DocumentReady() {
webBrowser1.Document.GetElementById("publishButton-ns").InvokeMember("Click");
}
Try a combination of @adam's suggestion and capitalize Click
尝试结合@adam的建议并大写
Click
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document
.GetElementById("ctl00_main_LoginExpoPlanIt_LoginButton")
.InvokeMember("Click");
}
Just tested this and it didn't work with "click" but did with "Click":)刚刚对此进行了测试,它不适用于“点击”,但可以使用“点击”:)
I'm using .net 4我正在使用 .net 4
EDIT: This only applies when runat="server"
is set, not applicable in this case but leaving for others just in case, my apologies on missing that in the question.编辑:这仅适用于设置
runat="server"
时,在这种情况下不适用,但留给其他人以防万一,我很抱歉在问题中错过了这一点。
ASP.Net changes the name of elements it renders based on the structure they are in, you can try the following to get the final name of the element: ASP.Net 会根据它们所在的结构更改其呈现的元素的名称,您可以尝试以下方法来获取元素的最终名称:
webBrowser1.Document.GetElementById("<%=publishButton-ns.ClientID%>").InvokeMember("click");
You can use jQuery and then do something like this $("#publishButton-ns").click();你可以使用 jQuery 然后做这样的事情 $("#publishButton-ns").click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.