Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to Click Button automatically via WebBrowser

The Html code of my click page is :

<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.

like image 719
Misha Avatar asked Jun 03 '11 22:06

Misha


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


4 Answers

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");  
   }  
 }
like image 51
Ponmalar Avatar answered Oct 04 '22 13:10

Ponmalar


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:

static void form1_Load() {
    // ...
    webBrowser1.onDocumentReady += webBrowser_DocumentReady;
}

static void webBrowser1_DocumentReady() {
    webBrowser1.Document.GetElementById("publishButton-ns").InvokeMember("Click");
}
like image 26
kirb Avatar answered Oct 04 '22 13:10

kirb


Try a combination of @adam's suggestion and capitalize 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

like image 33
MikeM Avatar answered Oct 04 '22 13:10

MikeM


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.

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:

webBrowser1.Document.GetElementById("<%=publishButton-ns.ClientID%>").InvokeMember("click");
like image 37
Guvante Avatar answered Oct 04 '22 14:10

Guvante