How to click a button in an webpage

回眸只為那壹抹淺笑 提交于 2019-12-11 02:47:53

问题


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.

I need to click a button called mImgBtnGo before, to download the file of the given date

in the mTbdate.

I don't know what I should do to click the button called mImgBtnGo .

What I should write here

              forms["mImgBtnGo"] = "?";

回答1:


using fiddler I think this is what you want:

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

     }


来源:https://stackoverflow.com/questions/8644433/how-to-click-a-button-in-an-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!