1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

c# need help with watin

Discussion in 'C, C++, C#' started by critsab, Aug 4, 2013.

  1. critsab

    critsab Newbie

    Joined:
    Mar 22, 2012
    Messages:
    15
    Likes Received:
    6
    Occupation:
    Software Developer
    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...
     
  2. DarkPixel

    DarkPixel Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 4, 2011
    Messages:
    1,328
    Likes Received:
    1,239
    Location:
    ↓↓↓↓
    Home Page:
    I don't use watin or C#, but you could use regex to access areas in the html you can't access with watin.
     
  3. milkyway

    milkyway Newbie

    Joined:
    Jul 18, 2013
    Messages:
    10
    Likes Received:
    1
    you should use a web debugger to see requests. in fact, this field can be not used while sending form field.
     
  4. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    Use find by class method and search for "textarea" which will return that element.
     
  5. critsab

    critsab Newbie

    Joined:
    Mar 22, 2012
    Messages:
    15
    Likes Received:
    6
    Occupation:
    Software Developer
    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();
     
  6. saxgod

    saxgod Regular Member

    Joined:
    Sep 19, 2010
    Messages:
    351
    Likes Received:
    337
    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&sender=me@me.com&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
     
  7. critsab

    critsab Newbie

    Joined:
    Mar 22, 2012
    Messages:
    15
    Likes Received:
    6
    Occupation:
    Software Developer
    Yess thank you for help but this site is very weird. I could do it if I had names but thanks for help anyway.. :)
     
  8. Piotr__11

    Piotr__11 Junior Member

    Joined:
    Mar 13, 2012
    Messages:
    122
    Likes Received:
    69
    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;                        
    }
    }
     
  9. aymenbnr

    aymenbnr Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 14, 2009
    Messages:
    200
    Likes Received:
    75
    Home Page:
    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 :)
     
  10. botrockets

    botrockets Regular Member

    Joined:
    Mar 16, 2013
    Messages:
    272
    Likes Received:
    463
    Occupation:
    Software Developer
    Location:
    Saint T.N.
    var elements = browser.Find("");
    foreach(var ele in elements)
    {
    //Get attributes
    //And then check their existence and then exit loop
    }
     
  11. masush300

    masush300 Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 16, 2012
    Messages:
    1,019
    Likes Received:
    333
    Occupation:
    Coding
    Location:
    Imagine Dragons
    Home Page:
    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
    }
    }
     
  12. neutralhatter

    neutralhatter Jr. VIP Jr. VIP Premium Member

    Joined:
    Jun 23, 2010
    Messages:
    430
    Likes Received:
    330
    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.