Clean up the website to get the name and item id via C # web browser

I am trying to clean the site to get information Textarea.

I use:

HtmlDocument doc = this.webBrowser1.Document;

When I look at the view source, it shows <textarea name="message" class="profile">

But when I try to access this text space with:

 HtmlDocument doc = this.webBrowser1.Document;

 doc.GetElementsByTagName("textarea")
      .GetElementsByName("message")[0]
      .SetAttribute("value", "Hello");

The error is displayed here:

 Value of '0' is not valid for 'index'. 'index' should be between 0 and -1.
Parameter name: index

Any help?

+5
source share
2 answers

For your current need, you can simply use this:

doc.GetElementsByTagName("textarea")[0].InnerText = "Hello";

For complex things, you can use the HtmlDocument class with the MSHTML class.

+2
source

I can trust HtmlAgilityPack to you!

, -, cookie, , ( ). , / , . ?

cookie, # ! ( )
cookie.

# -App , cookie/session, Cookies , , .
, . /, Fiddler, Tamper ..

. PostdataString: _ = TESTUSER & password = TESTPASSWORD & language = en & action% 3Asubmit = Submit

, .

        //Create the PostData
        string strPostData = "user_name=" + txtUser.Text + "&password=" + txtPass.Text + "&language=en&action%3Asubmit=Submit";
        CookieContainer tempCookies = new CookieContainer();
        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes(strPostData);

        //Create the Cookie
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.website.com/login.php");
        request.Method = "POST";
        request.KeepAlive = true;
        request.AllowAutoRedirect = false;
        request.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Referer = "http://www.website.com/login.php";
        request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1";
        request.ContentLength = data.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(data, 0, data.Length);

        HttpWebResponse response;
        response = (HttpWebResponse)request.GetResponse();
        string sRequestHeaderBuffer = Convert.ToString(response.Headers);

        requestStream.Close();

        //Stream(-output) of the new website
        StreamReader postReqReader = new StreamReader(response.GetResponseStream());

        //RichTextBox to see the new source.
        richTextBox1.Text = postReqReader.ReadToEnd();

Cookie , . -, . :.

        request.Headers.Add("Cookie", "language=en_US.UTF-8; StationID=" + sStationID + "; SessionID=" + sSessionID);
+1

All Articles