繁体   English   中英

在Winforms Webbrowser控件中无法获取htmlelement

[英]Unable to get htmlelement in winforms webbrowser control

我正在使用Winforms Webbrowser控件开发自动化程序。 我能够在前几个网页上获得htmlelement。 但是我无法从某些页面获取htmlelement,并且我正在使用与前几个Web页面相同的方法(成功获取了前几个htmlelement)。

        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");

它返回null,尽管这些元素存在于页面中,我也不知道为什么它返回null。 为什么以及该怎么做才能检测到html元素? 什么样的原因会导致其无法检测?

很久以前,当我最后一次尝试使用Web浏览器控件时,但是在旧版本的.net中,有一种方法可以使用这种方式:

HtmlElement createButton = this.extendedWebBrowser2.Document.Body.GetElementById("createButton");

公告Body

也许有帮助吗?

您正在等待文档加载事件吗? 在文档加载完成之前,有时DOM不可用。

您可以订阅浏览器控件上一个事件 如果您进行了所有处理,那应该没有问题。

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");      
        }
    }
}                  

无法获取HTML元素的原因是因为HTML元素嵌入在FrameSet中。 要访问FrameSet:

HtmlWindow docWindow = extendedWebBrowser2.Document.Window;

foreach (HtmlWindow frameWindow in docWindow.Frames)
{
  implementation code...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM