C# 模拟网页上的点击按钮
我必须访问一个网页,它有一个文本字段和一个“Go”按钮。在一个循环中,我给文本字段赋值,当我单击Go时,它会将我带到另一个网页。 文本字段的源代码如下所示C# 模拟网页上的点击按钮,c#,asp.net,C#,Asp.net,我必须访问一个网页,它有一个文本字段和一个“Go”按钮。在一个循环中,我给文本字段赋值,当我单击Go时,它会将我带到另一个网页。 文本字段的源代码如下所示 <td> <input name="ctl00$cphMyMasterCentral$ucSearch$txtNumber" type="text" value="40010" id="ctl00_cphMyMasterCentral_ucSearch_txtNumber" disabled="disabled"
<td>
<input name="ctl00$cphMyMasterCentral$ucSearch$txtNumber" type="text" value="40010" id="ctl00_cphMyMasterCentral_ucSearch_txtNumber" disabled="disabled" style="width:200px;" />
</td>
我有一个错误:
对象引用未设置为对象的实例
关于如何处理它的任何提示,谢谢您使用WebClient获取html,但WebBrowser从不知道,所以当您告诉他按ID获取元素时,WebBrowser当然不会喜欢它,不是吗?要做到这一点,您根本不需要webclient类。只需查找WebBrowser。导航,您将找到您需要的东西您在WebBrowser上的任何地方都没有导航过。如果您尝试使用Webbrowser b导航到您想要的页面,您可能会更幸运。你也可以去看看Watin。它是.Net的网页浏览附加组件,它使您尝试做的事情更加容易。
<a id="ctl00_cphMyMasterCentral_ucSearch_lbtnSearch" class="submit" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMyMasterCentral$ucSearch$lbtnSearch", "", true, "", "", false, true))'>Go</a>
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(mainURL);
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MS-RTC EA 2; MS-RTC LM 8; InfoPath.3)";
request.Timeout = 100000;
WebClient wc = new WebClient();
WebBrowser b =new WebBrowser();
string html = wc.DownloadString(mainURL);
b.Document.GetElementById("ctl00_cphMyMasterCentral_ucSearch_txtNumber").InnerText = "100";
b.Document.GetElementById("ctl00_cphMyMasterCentral_ucSearch_lbtnSearch").InvokeMember("click");