Unable to get htmlelement in winforms webbrowser control
I'm developing automation program using winforms webbrowser control. I am able to get the htmlelement at the first few web pages. But i'm unable to get the htmlelement from certain pages and i'm using the same method like wha开发者_运维技巧t i have done for the first few web pages (The few first pages htmlelement are retrieved successfully).
HtmlElement createButton = this.extendedWebBrowser2.Document.GetElementById("createButton");
HtmlElement textArea = this.extendedWebBrowser2.Document.GetElementById("query");
HtmlElement filename = this.extendedWebBrowser2.Document.GetElementById("filename");
HtmlElement cancelBtn = this.extendedWebBrowser2.Document.GetElementById("cancelBtn");
it returned null and i have no idea why it return null althought the elements exist at the pages. Why and what to do so i can detect the html element? What kind of reasons can cause it is unable to be detected?
It was long time ago when I last tried using webbrowser control, but in old versions of .net there was a way to get using this way:
HtmlElement createButton = this.extendedWebBrowser2.Document.Body.GetElementById("createButton");
Notice Body
.
Maybe it helps?
Are you awaiting the document load event? The DOM is sometimes unavailable until the document has finished loading.
There's an event on the browser control that you can subscribe to. If you do all your processing there it shouldn't be a problem.
if (objBrowser.Document.Window != null) {
foreach (HtmlWindow myframe in objBrowser.Document.Window.Frames) {
HtmlElementCollection htmlControls = myframe.Document.Body.GetElementsByTagName("OPTION");
foreach (HtmlElement optEle in htmlControls) {
optEle.SetAttribute("selected", "true");
}
}
}
The reason of unable to get the Html Elements is because the Html Elements are embedded in the FrameSet. To access the FrameSet :
HtmlWindow docWindow = extendedWebBrowser2.Document.Window;
foreach (HtmlWindow frameWindow in docWindow.Frames)
{
implementation code...
}
精彩评论