如何处理Geckofx中的弹出窗口

时间:2015-07-06 10:43:30

标签: c# popup geckofx

我不知道如何处理Gekofx中的弹出窗口。当弹出窗口出现时,我必须单击此弹出窗口中的按钮。 我尝试使用CreateWindow事件进行此操作,但我无法使其正常工作。

GeckoElementCollection followamf = webBrowser.Document.GetElementsByTagName("a");
        foreach (GeckoHtmlElement item in followamf)
        {
            string aux = item.GetAttribute("class");
            if (aux != null && aux != "" && aux.Equals("single_like_button btn3-wrap"))
            {
                webBrowser.CreateWindow += webBrowser_CreateWindow;
                item.Click();
                break;

            }
        }
void webBrowser_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
    {

         //This code doesn't work, because is search in main browser not in the popup and
    // I want to search in the popup. 
        GeckoElementCollection followtwitter = webBrowser.Document.GetElementsByTagName("button");
        foreach (GeckoHtmlElement item in followtwitter)
        {
            string aux = item.GetAttribute("class");
            if (aux != null && aux != "" && aux.Equals("button"))
            {

                item.Click();
                break;

            }
        }
    }

如果有人可以帮助我,我将非常感激。

2 个答案:

答案 0 :(得分:0)

根据您的代码,webBrowser变量是全局的,因此您的webBrowser_CreateWindow函数正在原始浏览器中搜索。

GeckoCreateWindowEventArgs中的webBrowser_CreateWindow参数包含WebBrowser属性,您可以将其设置为GeckoWebBrowser的新实例。事件处理程序返回后,您提供给GeckoWebBrowser的{​​{1}}实例将在其中加载弹出文档。您应该保存对新GeckoCreateWindowEventArgs的引用,为其添加GeckoWebBrowser事件的处理程序,然后检查它以查找要单击的元素。

此外,您应该只添加一次DocumentCompleted事件处理程序,否则将多次调用它。

答案 1 :(得分:0)

gecko.CreateWindow += Gecko_CreateWindow;
private void Gecko_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
    {
        if (!e.WebBrowser.Window.IsTopWindow())
            {
                 //your code here
                 //e.Cancel = true;//If you want to block popups
            }
    }