簡體   English   中英

GeckoWebBrowser如何標記驗證碼復選框(C#Windows窗體)?

[英]GeckoWebBrowser how to mark the Captcha checkbox (C# Windows forms)?

我正在使用GeckoWebBrowser開發Windows應用程序,並且試圖通過代碼檢查Captcha的復選框。 以編程方式,我已經可以獲取和設置html元素,但是無法達到此復選框。 我在頁面的任何地方都找不到。 我不是要嘗試驗證或驗證碼,只需檢查復選框元素,然后驗證是否已選中。 這樣簡單。

我目前所知道的:

在FireFox檢查器中,我可以看到 在此處輸入圖片說明 一些明顯的信息:驗證碼位於iframe中,標題為“ widget recaptcha”,寬度為304,高度為78。

此時checkbox元素(在iframe中): 在此處輸入圖片說明

現在,這就是我試圖獲取復選框的方式,以不同的方式查找id,span,div和class都沒有成功...

首先,在主文件中

            //looking all elements into main Document (around 1300 elements)
            GeckoElementCollection collection = geckoWebBrowser1.Document.GetElementsByTagName("*");
        foreach (GeckoHtmlElement elem in collection)
        {
            string id = elem.Id;
            if (id == "recaptcha-anchor")
            {
                string myId = "this is my ID";         //never find this ID!
            }
            //just for debug
            string LocalName = elem.LocalName;
            string OuterHtml = elem.OuterHtml;
            string TagName = elem.TagName;
            string TextContent = elem.TextContent;
            string role = elem.GetAttribute("role");
            string value = elem.GetAttribute("value");
        }

因此,在主文檔中,我無法通過ID找到任何內容。

接下來,查看iframe:

        //get the iframe works well
        foreach (GeckoIFrameElement iframe in geckoWebBrowser1.Document.GetElementsByTagName("iframe"))
        {
            //get main info about the iframe - ok
            string title = iframe.GetAttribute("title");
            if (title != null && title.ToLower().Contains("captcha"))   //got "recaptcha widget"
            {
                int x = iframe.OffsetLeft;
                int y = iframe.OffsetTop;
                int width = Convert.ToInt32(iframe.Width);
                int height = Convert.ToInt32(iframe.Height);
            }

            //inside the iframe, get all elements --> but always return null
            Gecko.Collections.IDomHtmlCollection<GeckoElement> collection2 = iframe.GetElementsByTagName("*");
            foreach (GeckoHtmlElement elem in collection2)
            {
                string id = elem.Id;
                string LocalName = elem.LocalName;
                string OuterHtml = elem.OuterHtml;
                string TagName = elem.TagName;
                string TextContent = elem.TextContent;
                string role = elem.GetAttribute("role");
                string value = elem.GetAttribute("value");
            }

            //foreach (GeckoHtmlElement elem in iframe.GetElementsByTagName("*"))             //get no elements
            //foreach (GeckoHtmlElement elem in iframe.GetElementsByTagName("input"))         //get no elements
            //foreach (GeckoHtmlElement elem in iframe.GetElementsByTagName("div"))           //get no elements
            foreach (GeckoHtmlElement elem in iframe.GetElementsByTagName("span"))           //get no elements
            {
                string id = elem.Id;
                string LocalName = elem.LocalName;
                string OuterHtml = elem.OuterHtml;
                string TagName = elem.TagName;
                string TextContent = elem.TextContent;
                string role = elem.GetAttribute("role");
            }
        }

因此,經過多次嘗試和錯誤后,我無法獲得checkbox元素,但是我可以獲取有關驗證碼框的一些信息,例如位置和大小,盡管標題不是我所期望的100%:在Firefox中,title = “ widget recaptcha”,並在GeckoWebbrowser標題=“ recaptcha小部件”中……有點奇怪。

這真讓我抓狂... :-(

有人在暗示我所缺少或我做錯了什么嗎? 有沒有辦法獲取所有html元素,甚至在iframe中還是完整的元素樹中?

可以做我想做的事嗎?

提前致謝!

要在當前頁面上找到所有iframe元素,請使用GeckoWindow的Frames屬性:

// this will return a collecton of all frames
var iframes = Browser.Window.Frames; 

我建議您在瀏覽器的DocumentCompleted事件的處理程序中執行此操作。 然后遍歷此框架。 每個框架都有自己的Document元素,實際上是其中所有元素的容器。 您的驗證碼應該在那里。 然后,您可能希望找到帶有復選框的div ,然后單擊它,這樣代碼將如下所示:

foreach (var iframe in iframes)
{
    var doc = iframe.Document;
    if (doc == null)
        continue;

    var elements = doc.GetElementsByClassName("your_name");

    foreach (var element in elements)
    {
        // get the div and validate it
        var myDiv = element as GeckoDivElement;
        if(myDiv == null || !myDiv.Id.Equals("your_checkbox_id", StringComparison.InvariantCultureIgnoreCase))
            continue;

        myDiv.Click(); // click your checkbox
        break;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM