如何使用WatiN单击浏览器“停止”按钮

时间:2011-09-12 08:02:31

标签: c# watin

我点击某个页面上的链接,然后在新标签页中打开。但该网站加载速度不快,并且有一些flash动画和一些其他元素加载很长时间。但是我所有必要的信息都会立即加载。所以我需要让页面停止加载。浏览器“停止”按钮会很好。我可以使用WatiN访问它吗?或者还有其他方法可以解决我的问题吗?感谢

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用httpwebrequest / webresponse?它应该只获得没有任何其他元素的页面(没有css没有图像没有flash动画) - 只是页面的代码而没有别的

答案 1 :(得分:0)

以下是一个例子:

private void GetPage(string protocol, string address, string extraPath, string[][] postData)
{
      try
      {
           long length = 0;
           string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(protocol + address + extraPath);
            httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
            httpWebRequest2.Method = "POST";
            httpWebRequest2.KeepAlive = true;
            httpWebRequest2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            httpWebRequest2.Host = address;
            httpWebRequest2.Referer = protocol + address;
            httpWebRequest2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30";
            httpWebRequest2.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
            httpWebRequest2.Headers.Add(HttpRequestHeader.AcceptLanguage, "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,pt-PT;q=0.2,en-GB;q=0.2");
            httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
            Stream memStream = new System.IO.MemoryStream();
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes(boundary + "\r\n");
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            length += boundarybytes.Length;
            boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            length += boundarybytes.Length;

            // Send any data via POST that you might need
            for (int i = 0; i < postData.length; i++;
            {
                 string head = "Content-Disposition: form-data; name=\"" + postData[i][0] + "\"\r\n\r\n";
                 byte[] headbytes = System.Text.Encoding.ASCII.GetBytes(head);
                 memStream.Write(headbytes, 0, headbytes.Length);
                 length += headbytes.Length;
                 head = postData[i][1];
                 headbytes = System.Text.Encoding.ASCII.GetBytes(head);
                 memStream.Write(headbytes, 0, headbytes.Length);
                 length += headbytes.Length;
                 memStream.Write(boundarybytes, 0, boundarybytes.Length);
                 length += boundarybytes.Length;
            }

            httpWebRequest2.ContentLength = memStream.Length;
            Stream requestStream = httpWebRequest2.GetRequestStream();
            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();
            WebResponse webResponse2 = httpWebRequest2.GetResponse();
            Stream stream2 = webResponse2.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2, Encoding.UTF8);
            string htmlpage = reader2.ReadToEnd();
            reader2.Close();
            webResponse2.Close();
            httpWebRequest2 = null;
            webResponse2 = null;
            // At this point, htmlpage contains all the code from the page ready to be handled
        }
        catch (WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                HttpWebResponse response = ex.Response as HttpWebResponse;
                if (response != null)
                {
                    // Got an error page from the server so save it and show it on browser
                    Stream stream2 = response.GetResponseStream();
                    StreamReader reader2 = new StreamReader(stream2, Encoding.UTF7);
                    StreamWriter writer = new StreamWriter("error.html");
                    writer.Write(reader2.ReadToEnd());
                    writer.Close();
                    reader2.Close();
                    response.Close();
                    response = null;
                    System.Diagnostics.Process.Start("error.html");
                }
            }
        }
    }

以这种方式调用它:

GetPage("http://", "www.google.com", "/search?sourceid=chrome&ie=UTF-8&q=stackoverflow", new strin[] {new string[]}); // in this case i send no data for post