[英]Specified cast is not valid. C# WebBrowser
url="https://ipv4.google.com/sorry/IndexRedirect?continue=https://www.google.com/search%3Fq%3Dstackoverflow%2B%26rlz%3D1C1KMZB_enTR561TR561%26oq%3Dstac%26aqs%3Dchrome.1.69i59l3j69i60j69i57j69i60.5208j0j7%26sourceid%3Dchrome%26espv%3D210%26es_sm%3D122%26ie%3DUTF-8";
webBrowser1.Navigate(url);
if (url.Contains("ipv4.google.com"))
{
frm2.ShowDialog();
Application.DoEvents();
webBrowser1.Document.GetElementById("captcha").SetAttribute("value", frm2.code);
webBrowser1.Document.GetElementById("submit").InvokeMember("click");
}
Exception is: 例外情况是:
Specified cast is not valid.
指定演员表无效。
at 在
webBrowser1.Document.GetElementById("captcha").SetAttribute("value", frm2.code);
I think that your webBrowser1 doesn't have the document loaded at the moment when you try to access it. 我认为您的webBrowser1在您尝试访问它时没有加载文档。 You should put your code in the if-clause into the eventhandler for
webBrowser1.Navigate
. 您应该将if-clause中的代码放入
webBrowser1.Navigate
的eventhandler中。 At this moment the webBrowser has fully loaded the document behind the url 此时webBrowser已将文档完全加载到URL后面
Use the document when it is ready loading. 准备好加载时使用该文档。
string url = "https://ipv4.google.com/sorry/IndexRedirect?continue=https://www.google.com/search%3Fq%3Dstackoverflow%2B%26rlz%3D1C1KMZB_enTR561TR561%26oq%3Dstac%26aqs%3Dchrome.1.69i59l3j69i60j69i57j69i60.5208j0j7%26sourceid%3Dchrome%26espv%3D210%26es_sm%3D122%26ie%3DUTF-8";
WebBrowser webBrowser1 = new WebBrowser();
this.Controls.Add(webBrowser1);
webBrowser1.Dock = DockStyle.Fill;
webBrowser1.Navigate(url);
if (url.Contains("ipv4.google.com"))
{
webBrowser1.DocumentCompleted += handler;
}
private void handler(object sender, WebBrowserDocumentCompletedEventArgs e)
{
frm2.ShowDialog();
webBrowser1.Document.GetElementById("captcha").SetAttribute("value", frm2.code);
webBrowser1.Document.GetElementById("submit").InvokeMember("click");
webBrowser1.DocumentCompleted -= handler;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.