Get an element in the deep HTML element stack

I am navigating to a site using an object WebBrowser

in C # and I want to get an HTML element that exists on that website, but that element is in a deep ugly stack of other elements, in fact the address is:

html → frameset → frame # mainFrame → html → body → div → table # AutoNumber → tbody → tr → td → div → form # lglform → table → tbody → tr → td → table → tbody → tr → td → input # Button1

when i try to use GetElementsByTagName("input")

i realize i need to go to the parent of that first first, and for that i need to get the parent too, etc.
is there a way to define a single HtmlElement

and set it to that element directly without going through all the parents?

UPDATE
I'm getting null in the GetElementById result because it WebBrowser

doesn't load the site correctly, even though I'm using an event DocumentCompleted

to make sure the website looks like this, like the inspector:

website in ie

but WebBrowser Object just loads this HTML

Vs
is there a way to make WebBrowser

Object to collect all HTML code?
or is there any other good way to get closer to my goal?

+3


source to share


2 answers


Since your input has an id Button1

, use GetElementById

. Otherwise, you will have to query webBrowser.Document.All

which is the collection of all elements in the HTML document.



HtmlElement input = webBrowser1.Document.GetElementById("Button1");

      

+1


source


You can use HtmlAgilitypack and get an element by its id Button1

:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(/*html data here*/);
var InputButton = doc.GetElementbyId("Button1");

      



// Edit: 1 somehow ignored the fact that you are using a webbrowser control, mattytommo's answer seems to be simpler and more appropriate.

0


source







All Articles