Simulate a Click on Webbrowser using C#

Asked

Viewed 1,608 times

2

I would like to know how to perform a click on a button of an internet site even by clicking on such web element debugged in the browser itself.

I’m using the webbrowser component of C#

I have tried some methods here, but without success, even using Getelementsbytagname

and the html of the site is like this.

<button type="button" ng-click="confirmarPresenca()">ENTRADA</button>

I tried with the following code but unsuccessfully in my Windows Form Application C#

HtmlElementCollection el = webBrowser1.Document.GetElementsByTagName("button");
foreach(HtmlElement btn in el)
{
    if(btn.GetAttribute("type").Equals("button")
    {
         btn.InvokeMember("click");
    }
}
  • Which version of Asp.net vc are you using? It’s webforms or MVC?

  • 1

    please specify your context better, using the Webbrowser component of C# or in a web browser?

  • In my answer, I inputei "absolutely nothing to do with C#" - which may be a misunderstanding. Looking at the nomenclature he used (Webbrowser + C#), it looks so much like Webforms - that I never worked. I’m waiting for an update from him to go ahead.

  • It would be a webbrowser component of c#. Look at an example link of how it would be more or less my doubt, but the html part of the site would be equal to the one I mentioned above http://answall.com/questions/17003/simular-click-no-webbrowser-usando-c

  • Did you ever debug the code to try to identify where exactly the problem is? You could check: (1) if el contains something, (2) what GetAttribute("type") returns and (3) if this return is equal to "button" tiny, and share in the question. It would make it a bit easier for someone to help you. :)

  • Dude I don’t understand I may be half a poof but that would be it: You want to click a button set on aspx by means of code Behind?

Show 1 more comment

2 answers

1

It would be in the case of clicking on a button of a specific WEB URL. The first button had the following HTML part

<input type="submit" value="Entrar" name="commit"></input>

the code to be able to click this button as follows

//Clica no Botão Entrar Após Preenchimento dos Campos
HtmlElementCollection doc1 = this.webBrowser1.Document.GetElementsByTagName("input");
foreach(HtmlElement doc00 in doc1)
{
    //Opção 2
    if(doc00.GetAttribute("type").Equals("submit"))
    {
        doc00.InvokeMember("click");
    }
}

But after he logs into the certain URL, which was filled in the login and password fields, there is another page that he enters, containing the following snippet of HTML code, which is a button too and wanted to invoke a click on it.

<button ng-click="confirmarPresenca()" type="button"></button>

But as mentioned above, I tried to take the button and attribute element and I had no return, and wanted some method of I could invoke a click on this button according to this HTML above

1

SNOT, you can use the Apis HtmlAgilityPack or even CsQuery, among others, in your project. If you consult in NUGET you will find them. They make it possible to use CSS search to rescue DOM objects.

But if in case you are going to continue using only the System.Windows.Forms.WebBrowser, i state to use the GetElementsByTagName("button") and by array get to the element you want to run the click.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.