我正在编写一个抓取程序。
我第一次能够单击锚标签,但是一旦我再次循环,就不会发生这种情况。我已经在 IE 的 Watin 实例中做到了这一点。我怀疑这是因为我所做的 IE 实例的背面。
请找到这两段代码。
下载PDF的方法
private void DownloadFiles(IE ieInstance, HTMLDocument document_sub)
{
// int chktest = 2;
try
{
foreach (HTMLAnchorElement anchorTagPg1 in document_sub.getElementsByTagName("a"))
{
if (anchorTagPg1.innerText.Contains("Monthly Statement"))
{
string urltemp = anchorTagPg1.outerHTML.ToString();
int startTextpos = urltemp.IndexOf("window.open");
string spltstr1 = urltemp.Substring(startTextpos);
int endpos = spltstr1.IndexOf(')');
string spltstr2 = spltstr1.Substring(0, endpos);
spltstr2 = spltstr2.Replace("window.open(", "");
string[] manipulatestrsplt = spltstr2.Split(',');
string finalmanipstr = manipulatestrsplt[0].Replace("'", "");
finalmanipstr = finalmanipstr.Replace("amp;", "");
finalurl = "https://myaccount.pseg.com/psegbdisu/" + finalmanipstr;
//anchorTagPg1.click();
//ieInstance.WaitForComplete();
ieInstance.GoToNoWait(finalurl);
Thread.Sleep(5000);
SendKeys.SendWait("^%S"); // to get the Save As window...
Thread.Sleep(5000);
SendKeys.SendWait("abc123");
Thread.Sleep(5000);
SendKeys.Send("^S");
Thread.Sleep(5000);
SendKeys.SendWait("DownloadedFile" + iFileCnt + ".pdf"); // Enters File Name
Thread.Sleep(5000);
SendKeys.Send("{ENTER}");
iFileCnt++;
//chktest++;
Thread.Sleep(5000);
ieInstance.Back();
// bool rtnBack = ieInstance.Back();
Thread.Sleep(10000);
// }
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
另一种方法,其中我无法点击锚标记,也无法引发 onclick 事件。
private void ClickSelectAccount(IE ieInstance)
{
FrameCollection hframesSelAcct = ieInstance.Frames;
HTMLDocument document_sub_SelAcct = ((mshtml.HTMLDocument)(((WatiN.Core.Native.InternetExplorer.IEDocument)(((WatiN.Core.FrameCollection)(hframesSelAcct))[1].NativeDocument)).HtmlDocument));
foreach (HTMLAnchorElement anchorTagSelectAcct in document_sub_SelAcct.getElementsByTagName("a"))
{
if (anchorTagSelectAcct.innerText.Contains("Select Account"))
{
Thread.Sleep(2000);
//anchorTagSelectAcct.click();
anchorTagSelectAcct.FireEvent("onclick");
Thread.Sleep(10000);
ieInstance.WaitForComplete();
anchorTagSelectAcct.click();
Thread.Sleep(10000);
break;
}
}
}
可以请人帮助我,第二种方法第一次效果很好,但第二次不行。我对 IE 实例有疑问,因为我尝试过更多的睡眠时间,并且它能够进入循环但无法单击链接。
提前致谢