I m trying to automate logging into a website using Python’s mechanize module.Following is the login form code:
<TD><B>Site:</B></TD>
<TD><SELECT name="site" class="drpdwn" size=1 width="200" onChange="populateZones(this, '');"><OPTION value="">Select a site</OPTION></SELECT>
<INPUT type="hidden" name="shortsite" size="3" value=""></TD>
</TR>
<TR>
<TD><B>Zone:</B></TD>
<TD><SELECT name="zone" class="drpdwn" size=1 width="100"><OPTION value="">Select a zone</OPTION></SELECT></TD>
</TR>
<TR>
<TD><B>User ID:</B></TD>
<TD><INPUT type="text" name="user" size=20 value=""></TD>
</TR>
<TR>
<TD><B>Password:</B></TD>
<TD><INPUT type="password" name="password" size=20></TD>
</TR>
<TR>
<TD> </TD>
<TD><INPUT name="submit1" type="submit" value="Log On" style="color: #003399; font-weight: bold; font-size: 10pt">
<input name="reset1" type="reset" value="Reset" style="color: #003399; font-weight: bold; font-size: 10pt" onClick="resetform();">
</TD>
I m able to parse and populate user, password and shortsite fields without any difficulty. The problem comes in while populating the zone drop down list. As is evident from the HTML code above that the webpage is using populateZones javascript function to populate the zones. I m looking for a way to force the zone value as stored in the configuration file. I have done similar things in the past using HTML::Form::ForceValue.Is there any equivalent in python?
Below is an excerpt from the python script that i m working on to automate the login:
br['user'] = self.config['COMMON.USER']
br['password'] = self.config['COMMON.PASSWORD']
if 'COMMON.SITE' in self.config and 'COMMON.ZONE' in self.config:
try:
br.form.set_all_readonly(False)
br.form["shortsite"] = self.config['COMMON.SITE']
br.form.find_control(name="zone").set_value_by_label([self.config['COMMON.ZONE']])
except ControlNotFoundError, err:
logger = logging.getLogger(__name__)
logger.info(str(err))
I was able to get this going by emmulating the javascript. Sharing the code as might be of some help: