【问题标题】:error using "webBrowser1.Document.Window.Frames"使用“webBrowser1.Document.Window.Frames”时出错
【发布时间】:2015-08-24 21:07:42
【问题描述】:

当使用以下代码行尝试访问 webBrowser 控件中 iFrame 中的链接时...

string Punch = webBrowser1.Document.Window.Frames("ContentFrame").Document.GetElementById("btnPunch");

我收到以下错误,“框架”一词下方的红色波浪线。

不可调用的成员“System.Windows.Forms.HtmlWindow.Frames”不能像方法一样使用。

Google 今天不是我的朋友...

添加了以下内容...

我做了以下更改,发现我定位的是框架而不是元素...

        HtmlElement elem = webBrowser1.Document.Window.Frames["ContentFrame"].Document.GetElementById("btnPunch");

        if (webBrowser1.Document.Window.Frames == null)
        {
            MessageBox.Show("No frame found");
        }
        else
        {
            MessageBox.Show("Frame found");
        }

        if (elem == null)
        {
            MessageBox.Show("elem is empty");
        }
        else
        {
            MessageBox.Show("elem is not empty");
        }

如果我能找到我想要点击的元素,我会添加

.InvokeMember("click");

到最后激活链接...

【问题讨论】:

    标签: c# visual-studio-2010 iframe webbrowser-control


    【解决方案1】:

    System.Windows.Forms.HtmlWindow.FramesHtmlWindowCollection 类型的属性。您可以像这样获得所需的框架:

    webBrowser1.Document.Window.Frames["ContentFrame"]
    

    因为您使用的是圆括号而不是方括号,编译器认为您正在尝试在 Window 对象上调用名为 Frames 的方法。

    【讨论】:

    • 我也试过这样。现在我收到以下错误...无法将类型'System.Windows.Forms.HtmlElement'隐式转换为'string'
    • 这是因为您的表达式(“=”符号右侧的内容)计算结果为 System.Windows.Forms.HtmlElement,但您试图将其分配给类型为 string 的变量(您的变量“冲床”)。也许你想要GetElementById("btnPunch").InnerText? (此外,您可能应该进行空值检查,除非您知道文档将始终具有您期望的确切结构。)
    • 我不知道 innerText 是我要找的。我在我的代码中的其他位置使用了 innerHtml,点击链接非常成功。但这一个在 iFrame 中,让我头疼。我已向 OP 添加了更多信息。我根据我们的谈话尝试过的事情。我希望它有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多