How to I automatcially fill out a 'textarea' in a webpage in python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fordie1000
    New Member
    • Mar 2008
    • 32

    How to I automatcially fill out a 'textarea' in a webpage in python?

    Hi,

    I have been trying to do this for a few days now ... I'd appreciate any help or insights ...

    Basically I want to enter text into a web 'textarea' automatically ... here is what I
    thought I should have ...

    import urllib
    import urllib2
    import cookielib

    url2 = "someUrl"
    url = "someOtherU rl"

    #Create empty cookie jar.
    cj = cookielib.LWPCo okieJar()
    #Install cookie handler for urllib2.
    opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cj))
    urllib2.install _opener(opener)


    request = urllib2.Request (url, None)
    f = urllib2.urlopen (request)
    f.close()

    # this is just to log into the site first ...
    data = urllib.urlencod e({"username": "xxxxx", "password" : "somePassword"} )
    request = urllib2.Request (url, data)
    f = urllib2.urlopen (request)

    html = f.read()
    #print html
    #print data
    f.close()


    request2 = urllib2.Request (url2, None)
    t = urllib2.urlopen (request2)
    t.close()
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    headers = { 'User-Agent' : user_agent }
    #Parse the html here (html contains the page markup).
    data2 = urllib.urlencod e({"message" : "rows5>blah ", "recipients[0]" : "xxxxxxx"})
    #print data2
    request2 = urllib2.Request (url2, data2, headers)
    t = urllib2.urlopen (request2)
    html2 = t.read()
    print html2
    t.close()


    but I'm guessing that 'textarea' doesn't have a value attribute .. it looks like the text just goes between the two tags <textarea></textarea>

    So how do I go about submitting predetermined text into the textarea? Do I have to parse the page and replace the whole 'textarea' section including my inputted text?

    Hope you can help me,
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by fordie1000
    [CODE=python]import urllib
    import urllib2
    import cookielib

    url2 = "someUrl"
    url = "someOtherU rl"

    #Create empty cookie jar.
    cj = cookielib.LWPCo okieJar()
    #Install cookie handler for urllib2.
    opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cj))
    urllib2.install _opener(opener)


    request = urllib2.Request (url, None)
    f = urllib2.urlopen (request)
    f.close()

    # this is just to log into the site first ...
    data = urllib.urlencod e({"username": "xxxxx", "password" : "somePassword"} )
    request = urllib2.Request (url, data)
    f = urllib2.urlopen (request)

    html = f.read()
    #print html
    #print data
    f.close()


    request2 = urllib2.Request (url2, None)
    t = urllib2.urlopen (request2)
    t.close()
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    headers = { 'User-Agent' : user_agent }
    #Parse the html here (html contains the page markup).
    data2 = urllib.urlencod e({"message" : "rows5>blah ", "recipients[0]" : "xxxxxxx"})
    #print data2
    request2 = urllib2.Request (url2, data2, headers)
    t = urllib2.urlopen (request2)
    html2 = t.read()
    print html2
    t.close()[/CODE]

    but I'm guessing that 'textarea' doesn't have a value attribute .. it looks like the text just goes between the two tags <textarea></textarea>
    What is the textarea that you speak of?

    Comment

    • fordie1000
      New Member
      • Mar 2008
      • 32

      #3
      Originally posted by jlm699
      What is the textarea that you speak of?
      Sorry I should have said ... the textarea i'm speaking about is an area
      in a web-form that will accept alot of text (depending on the limits the
      web-master has set). The html code for this is :

      Code:
      <div class="form-row">
      	<div class="form-col-a"><label for="message">Message </label></div>
      	<div class="form-col-b"><textarea style="OVERFLOW: auto"
      		wrap="SOFT" name="message" tabindex="1" id="message" cols="25"
      		rows="5"></textarea></div>
      Usually the textarea 'value' goes just before the "</textarea>" part ... but I want to
      fill this in automatically ... usually you can do this for a text input field using 'urllib.urlenco de' and 'urllib2.Reques t' but I don't think this will work for a textarea and I was wondering if anyone else has done this?

      It seems like it should be pretty easy to do but I am stumped!

      Thanks

      Comment

      • woooee
        New Member
        • Mar 2008
        • 43

        #4
        A link to a Python program that claims to do this. I haven't tried it but have wondered how well it works. http://wwwsearch.sourceforge.net/ClientForm/ Please post back on whether it works or not.

        Comment

        • fordie1000
          New Member
          • Mar 2008
          • 32

          #5
          Originally posted by woooee
          A link to a Python program that claims to do this. I haven't tried it but have wondered how well it works. http://wwwsearch.sourceforge.net/ClientForm/ Please post back on whether it works or not.
          Hi,

          Thanks for the reply ... I finally got this working ... thanks to ClientForm ... this
          is a great module ... very easy to use.

          Basically, I wanted to log into a website (using password and username) navigate to a page and enter details into a form and submit it. I wanted to do this so I could send sms text messages from the command line (without a browser open).

          Here is the code that I wrote to do the task ...
          any comments/suggestions are welcome :

          You run it from command line using : python progname.py "your message" number

          Code:
          #!/scisoft/bin/python2.4
          
          import time
          import urllib
          import urllib2
          import cookielib
          import ClientForm
          from HTMLParser import HTMLParser
          import sre
          
          #----------------- START WEBSITE URLS  ---------------------------#
          
          url = "www.someURL.com/login"
          url2 = "www.someURL.com/webtext/index.jsp"
          url3 = "www.someURL.com/myv/messaging/webtext/Process.shtml"
          
          
          #----------------- START USER VARIABLES --------------------------#
          
          etag = "None"
          user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT"
          protocol = 'https://'
          username = YOUR_USERNAME
          password = YOUR_PASSWORD
          maxMesgLen = 160
          
          #--------------------------- END ---------------------------------#
          
          
          def sendSMS(textmessage,*number):
                  """
          
                  """
          
                  print "Sending ........"
          
                  lenMessage = countCharacters(textmessage)
                  # Check to see if message is less than 160 characters
                  overFlowText = lenMessage - maxMesgLen
                  if lenMessage <= maxMesgLen:
          
          
                          #Create empty cookie jar.
                          cj = cookielib.LWPCookieJar()
          
                          opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
                          urllib2.install_opener(opener)
          
                          request1 = urllib2.Request(protocol+url, None)
                          t1 = opener.open(request1)
                          forms = ClientForm.ParseResponse(t1, backwards_compat=False)
                          t1.close()
          
                          form = forms[1]
                          form["username"] = username
                          form["password"] = password
                          # Check the keep me logged on checkbox
                          form.find_control("keeplogon").items[0].selected = True
                          #print form
          
                          request11 = form.click()
                          request11.add_header("User-Agent", user_agent)
                          response11 = opener.open(request11)
          
          
          
                          request2 = urllib2.Request(protocol+url2, None)
                          t = opener.open(request2)
                          #print t.geturl()
                          #print t.info()
                          #print "This is the code for the webtext page: ",t.code
                          forms = ClientForm.ParseResponse(t, backwards_compat=False)
                          t.close()
          
          
                          form = forms[1]  
                          form["message"]  = textmessage
          
                          numberList = list(*number)
                          noArgs = len(numberList)
                          for i,recip in enumerate(numberList):
                                  form["recipients[%d]" % i] = recip
          
          
                          request3 = form.click()
          
                          request3.add_header('User-Agent', user_agent)
                          request3.add_header('Referer', protocol+url2)
                          request3.add_header('Charset', "utf-8")
                          request3.add_header('If-None-Match', etag)
                          request3.add_header('Content-type', "text/html")
          
                          #This is vital for the message to send correctly!!!
                          time.sleep(2)
          
                          response2 = opener.open(request3)
          
                          # Was your message sent successfully???
                          success = response2.geturl()
                          recipList = []
                          if success == protocol+url3 :
                                  for recip in numberList:
                                          if recip != "":
                                                  recipList.append(recip)
                                  print "Your message was sent successfully to : ", recipList
                          else :
                                  print "Your message was _NOT_ sent!!!!!!"
                          #print response2.info()  # headers
                          #print 'This is the code :', response2.code
                          ##print response2.read()
                          response2.close()
          
          
          
                          # Get number of remaining Messages ... left
                          reqMesgRem = urllib2.Request(protocol+url2, None)
                          req = urllib2.urlopen(reqMesgRem)
                          dataMesgRem = req.read()
                          # find the number of remaining text messages left and the renewal date
                          matches = sre.findall('<span class="msg-total">(.*?)</span>', dataMesgRem)
                          print " "
                          print "# Messages Remaining -----> ", matches[0]
                          print "Renewal Date is      -----> ", matches[1]
                          req.close()
          
                  else :
                          print "Message too long by", overFlowText,"!"
          
          
          def countCharacters(message):
                  """
                  Counts the number of characters in the message
                  """
          
                  charCount= []
                  for char in message:
                          charCount.append(char)
                  messageLength = len(charCount)
                  return messageLength
          
          
          
          if __name__ == "__main__":
                  import sys
                  if len(sys.argv) < 8:
                          sendSMS(sys.argv[1],sys.argv[2:])
                  else :
                          print "ERROR : You have entered too many numbers!!!"

          Comment

          Working...