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:
but WebBrowser Object just loads this HTML
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?
source to share
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");
source to share
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.
source to share