How to reference an html button in C#

Asked

Viewed 414 times

0

I have the following snippet of HTML code :

inserir a descrição da imagem aqui

This code belongs to a page of an Air OS radio (internet radio).

I need to reference this button inside a program made in Windows Forms and do a function so that it runs and the radio is restarted.

I could only get elements with id attribute, as follows:

HtmlElement username = webBrowser1.Document.GetElementById("username");

How do I reference this button that does not have "id" field? Remembering that the HTML code is not mine, I cannot edit it and put the "id" field".

I’m currently using the Webbrowser component, which would have some other way to access the radio and reboot without using Webbrowser?

Thank you.

  • <input ID="username" value="Yes, reboot!" type="Submit">

  • I can’t change the HTML code, it’s not mine.

  • You have to post more of the HTML. You’ll have to work exploring the hierarchy. I mean, it’s going to be necessary to find some "key" element and get the kids. Also, try [Edit] and be more specific in your question, it seems that you’re talking about ASP.NET MVC, I just understood that you want to manipulate by a web browser because I’m used to dealing with Winforms.

  • edited the question, I am using windowsforms and the webbrowser component to accomplish this task.

  • Just wanted to post more parts of HTML. This way can not help...

  • I just posted all the code

  • The text "Yes, reboot" will always be this?

  • will be yes! HTML code does not change.

Show 3 more comments

2 answers

3

Try to do it this way :

var button = myWebBrowser.Document.GetElementsByTagName("input")
         .Cast<HtmlElement>()
         .FirstOrDefault(m => m.GetAttribute("type") == "submit");

1

You can try to grab this "button" by the type..

var inputs = document.getElementsByTagName('input');

In that case you have the inputs screen. This can go wrong if you have more inputs besides what you want to select, then you would need some (other) unique identifier.

Browser other questions tagged

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