我没有想法......
大家好,我对C#和Html Documents / Elements几乎一无所知,使用DocumentCompleted时发生了一些奇怪的事情。这是DocumentCompleted代码和PostGame()代码:
private void WB_CHL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (Date != "")
PostGame();
else
this.Close();
}
private void PostGame()
{
HtmlDocument doc = null;
doc = WB_CHL.Document;
HtmlElementCollection inputelems = doc.GetElementsByTagName("input");
inputelems.GetElementsByName("subject")[0].SetAttribute("value", Date);
doc.GetElementById("text_editor_textarea").SetAttribute("value", Games);
HtmlElementCollection inputbutton = doc.GetElementsByTagName("input");
inputbutton.GetElementsByName("post")[0].Focus();
SendKeys.Send("{ENTER}");
}
以下是HTML源代码:
</head><body background="https://i39.servimg.com/u/f39/17/72/44/45/backgr10.jpg" bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000"><a name="top"></a><table class="bodylinewidth" width="80%" cellspacing="0" cellpadding="10" border="0" align="center"><tr><td class="bodyline"><table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td align="center" width="100%" valign="middle"><a href="/"><img src="https://i58.servimg.com/u/f58/17/72/44/45/ovechk14.jpg" id="i_logo" border="0" alt="Crazy Hockey League" vspace="1" /></a><br /><div class="maintitle">Crazy Hockey League</div><br /><span class="gen">CHL Franchise™.<br /> </span></td></tr></table><table cellspacing="0" cellpadding="0" border="0" align="center"><tr><td align="center"><a class="mainmenu" href="/"><img id="i_icon_mini_index" src="https://i38.servimg.com/u/f38/17/18/20/77/home15.png" border="0" hspace="0" alt="Accueil" title="Accueil" /></a><a class="mainmenu" href="/calendar"><img id="i_icon_mini_calendar" src="https://illiweb.com/fa/empty.gif" border="0" hspace="0" alt="Calendrier" title="Calendrier" /></a><a class="mainmenu" href="/faq"><img id="i_icon_mini_faq" src="https://illiweb.com/fa/empty.gif" border="0" hspace="0" alt="FAQ" title="FAQ" /></a><a class="mainmenu" href="/memberlist"><img id="i_icon_mini_members" src="https://i38.servimg.com/u/f38/17/18/20/77/membre12.png" border="0" hspace="0" alt="Membres" title="Membres" /></a><a class="mainmenu" href="/groups"><img id="i_icon_mini_groups" src="https://i38.servimg.com/u/f38/17/18/20/77/groupe11.png" border="0" hspace="0" alt="Groupes" title="Groupes" /></a><a class="mainmenu" href="/profile?mode=editprofile"><img id="i_icon_mini_profile" src="https://i38.servimg.com/u/f38/17/18/20/77/profil11.png" border="0" hspace="0" alt="Profil" title="Profil" /></a><a class="mainmenu" href="/privmsg?folder=inbox" rel="nofollow"><img id="i_icon_mini_message" src="https://i38.servimg.com/u/f38/17/18/20/77/mp11.png" border="0" hspace="0" alt="Messagerie" title="Messagerie" /></a><a class="mainmenu" href="/login?logout=1&tid=0162d102f01a1c63d64d8d517645b5ae&key=7a24f2" rel="nofollow" id="logout" ><img id="i_icon_mini_logout" src="https://i38.servimg.com/u/f38/17/18/20/77/off11.png" border="0" hspace="0" alt="Déconnexion [ Ogie ]" title="Déconnexion [ Ogie ]" /></a></td></tr></table><div style="clear: both;"></div><div id="page-body"><div id="emptyidcc" class="no-left"><table cellpadding="0" cellspacing="0" width="100%" class="three-col"><tbody><tr><td valign="top" width="0"><div id="emptyidleft"></div></td><td valign="top" width="100%"><div class='n99fdc1'><script src="https://partner.googleadservices.com/gampad/google_service.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
GS_googleAddAdSenseService("ca-pub-0339214227387817"); GS_googleEnableAllServices();//]]>
</script><script type="text/javascript">//<![CDATA[
GA_googleAddAttr("Ad_type", "text"); GA_googleAddAttr("User_type", "Regular_user");GA_googleAddAttr("Page_type", "Other_page");GA_googleAddAttr("Site_type", "Premium");GA_googleAddAttr("Gender", "Male");GA_googleAddAttr("AgeRange", "18-24");//]]>
</script><script type="text/javascript">//<![CDATA[
google_ad_slot="1505882794"; google_ad_channel="2442858061+9170118551+4515235772+8811049474+2562183790+8771478709+2207379944+8003782320"; google_ad_type="text"; google_color_border="ffffff"; google_color_link="000000"; google_color_bg="ffffff"; google_color_text="000000"; google_color_url="ffffff"; google_ui_features="rc:6"; google_font="Arial"; google_font_size="Small"; google_alternate_ad_url="https://www.adstune.com/personal_ads/?bg_color=ffffff&body_link=000000&text_color=000000&url_color=000000&dateid=749406&lang=fr&cat=369&force=1&flang=fr"; GA_googleAddAdSenseSlotAttr("728x90-top", "google_ad_slot", google_ad_slot); GA_googleAddAdSenseSlotAttr("728x90-top", "google_ad_channel", google_ad_channel); GA_googleAddAdSenseSlotAttr("728x90-top", "google_ad_type", "text"); GA_googleAddAdSenseSlotAttr("728x90-top", "google_color_border", google_color_border); GA_googleAddAdSenseSlotAttr("728x90-top", "google_color_link", google_color_link); GA_googleAddAdSenseSlotAttr("728x90-top", "google_color_bg", google_color_bg); GA_googleAddAdSenseSlotAttr("728x90-top", "google_color_text", google_color_text); GA_googleAddAdSenseSlotAttr("728x90-top", "google_color_url", google_color_url); GA_googleAddAdSenseSlotAttr("728x90-top", "google_ui_features", google_ui_features); GA_googleAddAdSenseSlotAttr("728x90-top", "google_font", google_font); GA_googleAddAdSenseSlotAttr("728x90-top", "google_font_size", google_font_size); GA_googleAddAdSenseSlotAttr("728x90-top", "google_alternate_ad_url", google_alternate_ad_url);//]]>
</script><script type="text/javascript">//<![CDATA[
GA_googleUseIframeRendering();//]]>
</script><script type='text/javascript'>//<![CDATA[
GA_googleFillSlotWithSize("ca-pub-0339214227387817", "728x90-top", 728, 90);//]]>
</script><br class="clear" /></div><div style="clear:both;"></div><style type="text/css">html body .n99fdc1,html body .n99fdc1 iframe,html body iframe[name="google_ads_frame"],html body .n99fdc1 iframe[name="google_ads_frame"],html body #wrap #page-body div.n99fdc1,html body #page-body .n99fdc1,html body div.n99fdc1{display:block!important;visibility:visible!important;float:none!important;left:inherit!important;top:inherit!important;right:inherit!important;bottom:inherit!important;margin:0 auto!important;filter:alpha(opacity=100)!important;-moz-opacity:1!important;-khtml-opacity:1!important;opacity:1!important;z-index:9999!important;text-align:center!important}html body .n99fdc1,html body .n99fdc1 iframe[name="google_ads_frame"]{width:728px!important;height:90px!important;min-height:90px!important}html body .n99fdc1 iframe{border:0px!important}html body iframe[name="google_ads_frame"]{width:100%!important;height:inherit!important;min-height:inherit!important}</style><div style="height:3px"></div><script src="https://illiweb.com/rsc/16/frm/extendedview.js" type="text/javascript"></script><form action="/post" method="post" name="post" enctype="multipart/form-data"><table width="100%" border="0" cellspacing="2" cellpadding="0" align="center"><tr><td><span class="nav"><a class="nav" href="/">Crazy Hockey League</a> :: <h1><a href="/c7-chl-simulation" class="nav"><span>CHL | Simulation</span></a></h1> :: <h1><a href="/f29-resultats" class="nav"><span>Résultats</span></a></h1></span></td></tr></table><table class="forumline" width="100%" border="0" cellspacing="1" cellpadding="0"><tr><th height="28" class="thHead" colspan="2" valign="bottom"><strong>Poster un nouveau sujet</strong></th></tr><tr><td class="row1" width="22%"><span class="gen"><b>Titre du sujet</b></span></td><td class="row2" width="78%"><span class="gen"><input class="post" style="width:450px" type="text" name="subject" value="" size="45" maxlength="255" tabindex="2" title="La longueur du titre de ce sujet doit être comprise entre 1 et 255 caractères" onkeypress="if (event.keyCode==13){return false}" /><script type="text/javascript">//<![CDATA[
input = document.getElementsByName('subject');//]]></script><select name="topic_color" onchange="input[0].style.color = topic_color.value" style="margin-left:10px"><option value="#660000" style="color:#660000;" >Rouge foncé</option><option value="#FF0000" style="color:#FF0000;" >Rouge</option><option value="#FF9933" style="color:#FF9933;" >Orange</option><option value="#663300" style="color:#663300;" >Marron</option><option value="#FFFF00" style="color:#FFFF00;" >Jaune</option><option value="#006600" style="color:#006600;" >Vert</option><option value="#666633" style="color:#666633;" >Olive</option><option value="#00FFFF" style="color:#00FFFF;" >Cyan</option><option value="#0000FF" style="color:#0000FF;" >Bleu</option><option value="#000099" style="color:#000099;" >Bleu foncé</option><option value="#6600FF" style="color:#6600FF;" >Indigo</option><option value="#999999" style="color:#999999;" >Gris</option><option value="#FFFFFF" style="color:#CCCCCC;" >Blanc</option><option value="#000000" style="color:#000000;" >Noir</option><option value="" style="color:;" selected="selected">Aucune</option></select><script type="text/javascript">//<![CDATA[
input[0].style.color = '';//]]></script></span></td></tr><tr><td class="row1" width="22%"><span class="gen"><b>Description</b></span></td><td class="row2" width="78%"><span class="gen"><input class="post" style="width:450px" type="text" name="description" value="" maxlength="255" onkeypress="if (event.keyCode==13){return false}" /></span></td></tr><tr id="parent_editor_simple" ><td class="row1" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="1"><tr><td><span class="gen" style="white-space: nowrap;"><b>Corps du message</b></span></td></tr><tr><td align="center" valign="middle"><table width="100%" border="0" cellspacing="0" cellpadding="5"><tr align="center"><td class="gensmall" colspan=""><div id="smileyContainer"><script type="text/javascript">function addSmileyPane() { $("#smileyContainer").html('<iframe name="smilies" src="/smilies.forum?mode=smilies_frame&t=1509143808" frameborder="0" scrolling="auto" marginwidth="0" marginheight="0" height="350" width="220" class="row1"></iframe>'); }</script></div></td></tr></table></td></tr></table></td><td class="row2" valign="top"><span class="genmed"></span><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td valign="top"><textarea id="text_editor_textarea" class="post" name="message" tabindex="3" wrap="virtual"></textarea> </td><td id="smileyContainer" valign="top"><script type="text/javascript">function addSmileyPane() { $("#smileyContainer").html('<iframe name="smilies" src="/smilies.forum?mode=smilies_frame&t=1509143808" frameborder="0" scrolling="auto" marginwidth="0" marginheight="0" height="350" width="220" class="row1"></iframe>'); }</script></td></tr></table></td></tr></table><table class="forumline" width="100%" border="0" cellspacing="1" cellpadding="0"><tr><td class="catBottom" colspan="2" align="center" height="28"><input type="hidden" name="lt" value="0" /><input type="hidden" name="auth[]" value="c1900dead7de9ece4c5a991c880e82f0" /><input type="hidden" name="auth[]" value="1233e4038430dbd924cc1448d5f042f3" /><input type="hidden" name="mode" value="newtopic" /><input type="hidden" name="f" value="29" /><input class="liteoption" type="submit" name="preview" value="Prévisualiser" tabindex="5" /> <input class="mainoption" type="submit" name="post" value="Envoyer" tabindex="6" accesskey="s" /></td></tr></table><br /><br />
所以问题是在执行帖子之前必须调用DocumentCompleted / PostGame()函数4次。
第一回合:遇到此行时,会抛出异常
inputelems.GetElementsByName("subject")[0].SetAttribute("value", Date);
System.ArgumentOutOfRangeException: Value of '0' is not valid for
'index'. 'index' should be between 0 and -1.
Parameter name: index
第二回合:第一回合发生同样的事情
第3回合:浏览所有PostGame()函数,但不会在论坛上发布。 (这部分代码似乎没有很好地切换)
HtmlElementCollection inputbutton = doc.GetElementsByTagName("input");
inputbutton.GetElementsByName("post")[0].Focus();
SendKeys.Send("{ENTER}");
第4回合:浏览所有PostGame()函数并在论坛上正确发布。
我对此都很陌生......我已经尝试调试并找到问题超过3个小时而无法找到它们/他们......请帮帮我!!!
谢谢你们!
答案 0 :(得分:0)
您没有发布完整的HTML,但我可以说它包含一些iframe
。 < - 1}}在文件加载完毕后会被调用。
但是如果HTML中有多个DocumentCompleted
,那么您有多个文档。
因此,当每个iframe
完成加载它所指向的文档时,它会被多次调用。
您可以检查事件处理程序中iframe
参数的Url
属性,以查看加载的文档。如果您只想处理主页面加载,您应该可以使用:
WebBrowserDocumentCompletedEventArgs