如何单击网页中的按钮 [英] How to click a button in an webpage
本文介绍了如何单击网页中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这些代码从服务器下载文件.
I'm using these code to download a file from the server.
我需要从中下载文件的服务器的链接是:
The link of the server from where I need to download the file is:
http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx
var forms = new NameValueCollection();
forms["__EVENTARGUMENT"] = "";
forms["__VIEWSTATE"] = ExtractVariable(s, "__VIEWSTATE");
forms["mTbdate"] = "12/22/2011";
forms["__EVENTVALIDATION"] = __EVENTVALIDATION;
forms["mImgBtnGo"] = "?";
forms["__EVENTTARGET"] = "btnLink_Excel";
webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
var responseData = webClient.UploadValues(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx", "POST", forms);
System.IO.File.WriteAllBytes(@"c:\11152011.csv", responseData);
它会在文本框中下载给定日期的文件,这是现在网站上的默认设置.
Its downloading the file of the given date in the textbox which is default in the website now.
我需要先单击一个名为mImgBtnGo的按钮,才能下载给定日期的文件
I need to click a button called mImgBtnGo before, to download the file of the given date
在mTbdate中.
我不知道如何单击名为mImgBtnGo的按钮.
I don't know what I should do to click the button called mImgBtnGo .
我应该在这里写什么
forms["mImgBtnGo"] = "?";
推荐答案
我认为这就是你想要
class Program
{
static string Extract(string s, string tag)
{
var startTag = String.Format("id=\"{0}\" value=\"", tag);
var eaPos = s.IndexOf(startTag) + startTag.Length ;
var eaPosLast = s.IndexOf('"', eaPos);
return s.Substring(eaPos, eaPosLast-eaPos);
}
static void Main(string[] args)
{
WebClient webClient = new WebClient();
var firstResponse = webClient.DownloadString(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx");
var forms = new NameValueCollection();
forms["__EVENTARGUMENT"] = "";
forms["__VIEWSTATE"] = Extract(firstResponse, "__VIEWSTATE");
forms["mTbdate"] = "12/22/2011";
forms["__EVENTVALIDATION"] = Extract(firstResponse, "__EVENTVALIDATION");
forms["mImgBtnGo.x"] = "10";
forms["mImgBtnGo.y"] = "10";
forms["ScriptManager1"] = "MupdPnl|mImgBtnGo";
// forms["__EVENTTARGET"] = "btnLink_Excel";
webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
String secondResponse = UTF8Encoding.UTF8.GetString(
webClient.UploadValues(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx", "POST", forms)
);
forms = new NameValueCollection();
forms["__EVENTARGUMENT"] = "";
forms["__VIEWSTATE"] = Extract(secondResponse, "__VIEWSTATE");
forms["mTbdate"] = "12/22/2011";
forms["__EVENTVALIDATION"] = Extract(secondResponse, "__EVENTVALIDATION");
// forms["mImgBtnGo"] = "?";
forms["__EVENTTARGET"] = "btnLink_Excel";
webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
var responseData = webClient.UploadValues(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx", "POST", forms);
System.IO.File.WriteAllBytes(@"c:\prj\11152011.csv", responseData);
}
}
这篇关于如何单击网页中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文