【发布时间】:2017-10-15 19:15:02
【问题描述】:
我正在使用 GeckoWebBrowser 开发 Windows 应用程序,并且正在尝试通过代码检查验证码的复选框。以编程方式,我已经可以获取和设置 html 元素,但是我无法访问此复选框。我在页面的任何地方都找不到它。 我不是要破解或解决验证码,只需检查复选框元素,然后验证它是否被选中。就这么简单。
我目前所知道的:
在 FireFox 检查器中,我可以看到 一些明显的信息:验证码在一个 iframe 中,title="widget recaptcha",width=304 和 height=78。
现在,这就是我试图获取复选框的方式,以不同的方式寻找 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");
}
}
所以,经过多次尝试和错误后,我无法获得复选框元素,但我可以获得有关验证码框的一些信息,例如位置和大小,尽管标题不是我预期的 100%:在 Firefox 中title = "widget recaptcha" 和 GeckoWebbrowser title = "recaptcha widget"...有点奇怪。
这快把我逼疯了... :-(
有人对我缺少什么或我做错了什么有一些建议吗? 有没有办法在 iframe 或完整的元素树中获取所有 html 元素?
有可能做我想做的事吗?
提前致谢!
【问题讨论】:
标签: c# winforms checkbox captcha gecko