dimanche 15 octobre 2017

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

I have a Windows application with GeckoWebBrowser and I'm trying to check the checkbox of a Captcha via code. Programmaticaly I already can get and set html elements, but this checkbox I can't reach. I can't find it in any place in the page. I'm not trying to decifrate or solve the captcha, just check the checkbox element and after that verify if it is checked. Simple like that.

What I know at the moment:

In the FireFox inspector I can see enter image description here Some obvious informations: the captcha is in a iframe with title="widget recaptcha", width=304 and height=78.

The checkbox element is in this point (inside the iframe): enter image description here

Now, this is how I'm trying to get the checkbox, looking for id, span, div and class in different ways with no success...

First, in the main Document

            //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");
        }

So, in the main Document I can't find anything by ID.

Next, looking into 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");
            }
        }

So, after a lot of try and error I can't get the checkbox element, but I can get some info about the captcha box, like position and size, although the title is not 100% as I expected: in Firefox the title = "widget recaptcha" and in the GeckoWebbrowser title = "recaptcha widget"... a litle bizarre.

This is driving me crazy... :-(

Anybody has some sugestion what I'm missing or what I'm doing wrong? Is possible to do what I'm trying to do?




Aucun commentaire:

Enregistrer un commentaire