【问题标题】:Click Button in iframe in WebBrowser Control单击 WebBrowser 控件中 iframe 中的按钮
【发布时间】:2015-10-04 12:34:26
【问题描述】:

我正在使用 C#,win 应用程序,试图单击在我的网络浏览器的 iframe 中找到的按钮。

HTML:

<iframe frameborder="0" scrolling="no" id="EditorIFrmae" name="EditorIFrmae" width="100%" height="700" style="z-index: 0; height: 852px;" src="X"></iframe>

在 iframe 代码中:

<div height="" width="100%" style="position: relative; min-height: 50px;" onclick="javascript:funcEditPage('SB_Content_Page','SB_Content_PagePreview','PAGE5.asp');" class="SB_DivContentPreview" id="SB_Content_PagePreview">
</div>

我的代码:

HtmlElementCollection linkit = this.webBrowser1.Document.GetElementsByTagName("div");
            foreach (HtmlElement foundit in linkit)
            {
                if (foundit.GetAttribute("id").Equals("SB_Content_PagePreview"))
                {
                    foundit.InvokeMember("Click");
                }
            }

我怎样才能点击按钮?

【问题讨论】:

  • 非常不清楚的问题。具体是什么问题
  • @FarhanAnam - 对不起,这还不够清楚:我试图点击在 ifame 而不是当前页面上找到的 div。
  • 您的代码看起来不错您遇到了什么问题?
  • @FarhanAnam - 我猜问题是我的代码找不到 div,因为他在 iframe 页面中而不是在当前页面上。我得到的错误是 - div not found
  • 哦,所以按钮没有被点击。你在问题中没有提到这个事实。无论如何,回到问题,如果您不需要显示页面,则应该使用 HTML Agility Pack 而不是 WebBrowser。

标签: c# winforms iframe webbrowser-control


【解决方案1】:

您可以使用Document.Window.Frames[] 获取接受帧的 id(string) 或 index(int) 的帧。

您还应该考虑等到DocumentCompleted 事件引发后再做您的工作。当页面中有一些 iframe 时,DocumentCompleted 事件会触发不止一次,并使用下面的代码通过检查 eventarg 的 url 我们确保这是我们需要的主页的 DocumentCompleted 事件,然后您可以启用标记表示文档已完成,在这里或其他地方,找到你想要的框架和元素。

代码在哪里:

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        this.webBrowser1.Navigate(@"d:\test.html");
        this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }

    bool completed = false;
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (e.Url == webBrowser1.Document.Url)
        {
            completed = true;
        }
    }

    private void clickToolStripButton_Click(object sender, EventArgs e)
    {
        if(completed)
        {
            var frame = webBrowser1.Document.Window.Frames["iframeid"];
            var button = frame.Document.GetElementById("buttonid");
            button.InvokeMember("click");
        }
    }
}

test.html 的内容:

<html>
<head>
    <title>OUTER</title>
</head>
<body>
    Some Content <br/>
    Some Content <br/>
    Some Content <br/>
    iframe:
    <iframe src="test2.html" id="iframeid"></iframe>
</body>
</html>

test2.html 的内容:

<html>
<head>
    <title>IFRAME</title>
</head>
<body>
   <button id="buttonid" onclick="alert('Clicked')">Click Me</button> 
</body>
</html>

截图:

【讨论】:

    【解决方案2】:

    您必须等待 Document 完全加载,然后再尝试在其中查找元素,并且在加载 框架时,您必须在 框架内进行搜索 也是。
    订阅DocumentCompleted 活动

    initialUrl = new Uri(/* your url */);
    webBrowser.DocumentCompleted += OnDocumentCompleted;
    webBrowser.Navigate(initialUrl);
    

    然后尝试在方法中查找元素:

        private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs args)
        {
           if (args.Url == initialUrl)
            {
                HtmlElementCollection elements =
                    webBrowser.Document.Window.Frames[0].Document.GetElementsByTagName("div");
                foreach (HtmlElement element in elements)
                {
                    if (element.GetAttribute("id").Equals("SB_Content_PagePreview"))
                    {
                        element.InvokeMember("Click");
                    }
                }
            }
        }
    

    【讨论】:

    • 感谢您尝试帮助我,但没有多大帮助。
    • 是的。我确实错过了框架的使用。
    【解决方案3】:
     HtmlElementCollection linkit = this.webBrowser1.Document.GetElementsByTagName("iframe");
                foreach (HtmlElement foundit in linkit)
                {
                    if (foundit.GetAttribute("id").Equals("EditorIFrmae"))
                    {
                        string iframeurl = foundit.GetAttribute("src");
                    }
                }
    

    这是我用来获取 iframe 链接然后导航到该链接的选项。

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多