c# need help with watin

critsab

Newbie
Joined
Mar 22, 2012
Messages
15
Reaction score
6
Hello bhw members.

I wanted to make a bot for one web site but that web site doesnt contain neither id or name element in source code.

Can you guide me how I can access buttons and forms using something else maybe textarea..

this is how the source code looks that I want to access through c# ..


<textarea class="textarea" placeholder="Have your say..." style="overflow: hidden;"></textarea>


so it is text area and I want to input text there...
 
I don't use watin or C#, but you could use regex to access areas in the html you can't access with watin.
 
you should use a web debugger to see requests. in fact, this field can be not used while sending form field.
 
Use find by class method and search for "textarea" which will return that element.
 
Code:
<div class="module wide-module after-lead-story-module">
    <ul class="thumbnail">
           <li>
       <a href="link">
                <img alt="image" width="150" />

        
   
        
                        <span class='floating-comment small'><span class="floating-number">91</span></span>
       </a>
       <a href="link</a>
      </li>
           <li>
       <a href="link" class="image-with-caption">
                <img alt="Millsap_crop_north" height="100" src="image" width="150" />

So you can see there is no ID or Name for those links. So does any one have any kind of code to help me?


If there is ID then it would be easy to use
Code:
webBrowser1.Document.GetElementById("").SetAttribute();
 
If you are building a bot that fills in a form in C# you don't even need to parse the HTML or put anything in the HTML.
All you need to do is sent a POST request to the same page as where the form action leads to.

So suppose you have a form like this:

Code:
<form action="submit.php" method="post">
   Subject: <input type="text" name="subject"/><br/>
   Your Email Address: <input type="text" name="sender"/><br/>
   Message: <textarea name="msg"></textarea><br/>
</form>

You would write a bit of C# code like this:
Code:
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://example.com/submit.php");
            byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes("subject=this is the subject&[email protected]&msg=Hello! I am sending a message!");
            
            req.Method = "POST";
            req.ContentType = "application/json; charset=utf-8";
            req.ContentLength = requestBytes.Length;
            
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();


            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
            String response = sr.ReadToEnd();


            sr.Close();
            res.Close();

then you can do something with "response" and check if the submission went through by seeing if it contains key identifiers.

For more examples check out stackoverflow or google "c# webrequest post data" & "c# webrequest post fields"

Edit:
Oh IC, you don't have the name field for that textarea...
Wierd... Check javascripts to see if they do anything with it eg in jQuery or something
 
Yess thank you for help but this site is very weird. I could do it if I had names but thanks for help anyway.. :)
 
You can loop through the html elements and look for the one u re interested in :

PHP:
 foreach (HtmlElement item in webBrowser.Document.GetElementsByTagName("textarea"))             
       {               
         if (item.GetAttribute("placeholder").Equals("SOMETHINGGGG")) //u can modify the attribute name and try with other one                    
    {                           
 item.InvokeMember("click"); //click the item              
  //item.InnerText = "xxx"; fill with text etc               
 break;                        
}
}
 
if you are trying to access html tags and elements you can use htmlagilitypack , i've been using it for a long time ! pretty useful for bot makers :)
 
var elements = browser.Find("");
foreach(var ele in elements)
{
//Get attributes
//And then check their existence and then exit loop
}
 
I dont like watin

1.it cant be multithreaded
2.cant get attached to browser

answer: on c#(Not watin)

int a=0;
foreach (HtmlElement elm in webBrowser1.Document.All)
{
++a;
if (elm.GetAttribute("className") == "textarea" && a==1)
{
element.InvokeMember("click"); //clicks first button with class name text area
}
}
 
I've done extensive research on web automation and if you are going this direction I strongly recommend that you use Selenium instead of Watin.... it's much more mature.
 
Back
Top