如何点击iframe中的按钮/链接? (GeckoFx)

时间:2014-09-21 15:44:24

标签: c# geckofx

此代码访问iframe并获取源代码。

string content = null;
var iframe = browser.Document.GetElementsByTagName("iframe").FirstOrDefault() as Gecko.DOM.GeckoIFrameElement;
if (iframe != null)
{
    var html = iframe.ContentDocument.DocumentElement as GeckoHtmlElement;
    if (html != null)
        content = html.OuterHtml;

    textBox1.Text = content;
}

我尝试了一些代码

string content = null;
var iframe = browser.Document.GetElementsByTagName("iframe").FirstOrDefault() as Gecko.DOM.GeckoIFrameElement;
if (iframe != null)
{
    var html = iframe.ContentDocument.DocumentElement as GeckoHtmlElement;
    if (html != null)
        content = html.OuterHtml;

    textBox1.Text = content;

    GeckoElementCollection elements = browser.Document.GetElementsByName("username");
    foreach (var element in elements)
    {
        GeckoInputElement input = (GeckoInputElement)element;
        input.Value = "Auto filled!";
    }
}

但它不会工作,因为代码没有找到元素。有什么想法吗?

尝试在谷歌搜索任何iframe示例,但似乎没有任何好的文档。

1 个答案:

答案 0 :(得分:1)

为什么要在主文档中查找?你应该在一个框架中寻找。

string content = null;
var iframe = browser.Document.GetElementsByTagName("iframe").FirstOrDefault() as Gecko.DOM.GeckoIFrameElement;
if (iframe != null)
{
    var html = iframe.ContentDocument.DocumentElement as GeckoHtmlElement;
    if (html != null)
        content = html.OuterHtml;

    textBox1.Text = content;

    GeckoElementCollection elements = iframe.ContentDocument.GetElementsByName("username");
    foreach (var element in elements)
    {
        GeckoInputElement input = (GeckoInputElement)element;
        input.Value = "Auto filled!";
    }
}