0

im trying to access the rows in an html form, the html code in the form is as such

</script> 

<form name="calendarForm" method="post" action="/ibook/publicLogin.do" onsubmit="return validateForm(this);"><div><input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="7a6aa28270cc38601c894a05d01b7264"></div> 

   <input type="hidden" name="apptDetails.apptType" value="PRAP"> 

   <table border="0" cellspacing="1" cellpadding="0" align="center"> 
       <tr> 
         <td nowrap="nowrap" width="20%"><div id=id_div1 style="display:''"><FONT color=#ff0000>*</FONT><label for="apptDetails.identifier1">Sponsor's NRIC /<br />&nbsp;&nbsp;Applicant's FIN</label></div></td> 
         <td nowrap="nowrap" width="5%">:</td> 
         <td nowrap="nowrap" width="75%"><div id=id_div3 style="display:''"><input type="text" name="apptDetails.identifier1" maxlength="9" size="15" value="" onblur="javascript:this.value=this.value.toUpperCase();" id="apptDetails.identifier1" style="text-transform: uppercase;" class="txtFill_singleLine"></div></td> 
       </tr> 

im tring to add information to row with name name="apptDetails.identifier1" how do i input values to the row? i dont seem to be able to access the html row using conventional python mechanize form options please advise

here is my code

import cookielib 
import urllib2 
import mechanize 

# Browser 
br = mechanize.Browser() 

# Enable cookie support for urllib2 
cookiejar = cookielib.LWPCookieJar() 
br.set_cookiejar( cookiejar ) 

# Broser options 
br.set_handle_equiv( True ) 
br.set_handle_gzip( True ) 
br.set_handle_redirect( True ) 
br.set_handle_referer( True ) 
br.set_handle_robots( False ) 

# ?? 
br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1 ) 

br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0' ),('Host','eappointment.ica.gov.sg'),('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8') ] 

# authenticate 
br.open('https://eappointment.ica.gov.sg/ibook/gethome.do')

print "forms"
br.select_form(name="calendarForm")
print "forms"

# these two come from the code you posted
# where you would normally put in your username and password


br.find_control(name="apptDetails.apptType").value = ['PRAP']



res = br.submit()



html = br.response().readlines()
file = open("html.txt", "w")
for i in range(0, len(html)):
    file.write(html[i])
    file.write('\n')

file.close()    
br.close()
print "Success!\n"
Adhil
  • 1,678
  • 3
  • 20
  • 31

1 Answers1

0

After calling br.submit(), you will have navigated to the next page. There are two forms on that page, both named "calendarForm":

>>> for f in br.forms():
...     print f
...     print
... 
<calendarForm POST https://eappointment.ica.gov.sg/ibook/loginSelection.do application/x-www-form-urlencoded
  <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)>
  <SelectControl(apptDetails.apptType=[CS, CSCA, CSCR, CSIC, CSPC, CSXX, PR, *PRAP, PRCF, PRAR, PRIC, PRNN, PRTR, CSXX, VS, VSEI, VSLA, VSLT, VSSP, VSST, VSVP])>>

<calendarForm POST https://eappointment.ica.gov.sg/ibook/publicLogin.do application/x-www-form-urlencoded
  <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)>
  <HiddenControl(apptDetails.apptType=PRAP) (readonly)>
  <TextControl(apptDetails.identifier1=)>
  <TextControl(apptDetails.identifier2=)>
  <TextControl(apptDetails.identifier3=)>
  <IgnoreControl(Clear=<None>)>>

You need to select the second form (with index 1), so:

>>> br.select_form(nr=1)
>>> print br.form
<calendarForm POST https://eappointment.ica.gov.sg/ibook/publicLogin.do application/x-www-form-urlencoded
  <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)>
  <HiddenControl(apptDetails.apptType=PRAP) (readonly)>
  <TextControl(apptDetails.identifier1=)>
  <TextControl(apptDetails.identifier2=)>
  <TextControl(apptDetails.identifier3=)>
  <IgnoreControl(Clear=<None>)>>

Now you can fill in the form's fields:

>>> br.form['apptDetails.identifier1'] = '12345'
>>> br.form['apptDetails.identifier2'] = '99'
>>> br.form['apptDetails.identifier3'] = '911'
>>> print br.form
<calendarForm POST https://eappointment.ica.gov.sg/ibook/publicLogin.do application/x-www-form-urlencoded
  <HiddenControl(org.apache.struts.taglib.html.TOKEN=2eb104ffed4bc6ea09b67e556e5dd6e2) (readonly)>
  <HiddenControl(apptDetails.apptType=PRAP) (readonly)>
  <TextControl(apptDetails.identifier1=12345)>
  <TextControl(apptDetails.identifier2=99)>
  <TextControl(apptDetails.identifier3=911)>
  <IgnoreControl(Clear=<None>)>>

and, finally, submit and save the response for inspection:

>>> br.submit()
>>> open('response.html', 'w').write(br.response().read())

If you entered valid values for the 3 identifiers, you should be at the next page, whatever that is.

mhawke
  • 84,695
  • 9
  • 117
  • 138