Tuesday 15 February 2011

c# - Unable to click the anchor tags for the second time -



c# - Unable to click the anchor tags for the second time -

i writing scraping program.

the first time able click anchor tags, 1 time 1 time again loop in same doesn't happen. have done in watin instance of ie. uncertainty because of of ie instance have done.

please find 2 pieces of code.

downloading pdf's method

private void downloadfiles(ie ieinstance, htmldocument document_sub) { // int chktest = 2; seek { 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"); // save 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); // } } } } grab (exception ex) { messagebox.show(ex.message.tostring()); } }

another method in not able nail anchor tag, nor able raise onclick event.

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; } } }

can please help me, first time sec method works fine not sec time. have uncertainty ie instance, have tried more sleep time, , able go in loop not able click links.

thanks in advance

try next within downloadfiles method:

var link = ieinstance.links.firstordefault(p => p.text.equals("select account")); if (link != null && link.exists) { link.click(); }

or

var link = ieinstance.links.firstordefault(p => p.text.equals("monthly statement")); if (link != null && link.exists) { link.click(); }

debug code see getting within link.exists if statement. if not should try:

var links = browser.links; foreach (var link in links.where(l => l != null && (l.text.contains("select account")| l.text.contains("monthly statement")))) { link.click(); }

let me know if worked or not.

c# watin mshtml pdf-scraping

No comments:

Post a Comment