【发布时间】:2016-11-22 03:16:41
【问题描述】:
我想做的是以编程方式点击iframe,看起来像这样
html 代码是:
<div class="g-recaptcha" data-sitekey="6Lf3v8wSAAAAABVuu8qFKDtsvc0ILPoyUBVRToA9" style="width: 40%; margin: 0 auto;">
<div>
<div style="width: 304px; height: 78px;">
<iframe id="I0_1420487033543" width="304" height="78" frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" style="" tabindex="0" vspace="0" name="I0_1420487033543" src="https://www.google.com/recaptcha/api2/anchor?k=6Lf3v8wSAAAAA…=http%3A%2F%2F&pfname=&rpctoken=24572959"></iframe>
</div>
<textarea id="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 80px; border: 1px solid #c1c1c1; margin: 0px; padding: 0px; resize: none; display: none; " name="g-recaptcha-response" dir="ltr"></textarea>
</div>
</div>
而我想做的是:
private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (this.Web.ReadyState != WebBrowserReadyState.Complete)
return;
this.Web = sender as WebBrowser;
var theElementCollection = Web.Document.Body.GetElementsByTagName("iframe");
for (int i = 0; i < theElementCollection.Count; i++)
{
HtmlElement curElement = theElementCollection[i];
if (curElement.Id != null && curElement.Id.StartsWith("I0_"))
{
curElement.InvokeMember("click");
}
}
}
但它不起作用,我该如何解决?
【问题讨论】:
-
可能是内置的浏览器安全性阻止了点击(域不同)。
-
@mikeTheLiar:是的,顶级文档不应该能够自动化 iframe 的内容。但尽管如此,如果这是可能的,那么这个验证码将毫无用处,验证码的全部意义在于你不能自动化它们。