424,279 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

Using mechanize in python to navigate a website

P: 25
Hi all....

I'm using pyscripter 1.7.2, on a Win XP Prof 2002 - service pack 2 machine to script a website. I have no problem logging into the site, loading and an xml file, and retrieving the confirmation transaction code xml file, but when I go to the reports page and try to grab a report, I run into a problem. The "Get Report" button code within the web page is the following:

Expand|Select|Wrap|Line Numbers
  1. <input type="button" name="change" value="Get Report" onclick="getReport()" class="Button">
Now I can't do a br.submit() on this button, as mechanize doesn't recognize it as "clickable". The "getReport()" function is javascript and is coded as follows:

Expand|Select|Wrap|Line Numbers
  1. function getReport(){
  2.         var elm = getEventElm();
  3.         var form = getParent(elm,"form");
  4.         form.action = 'demandBids.do?method=report';
  5.         form.target = "_top";
  6.         form.submit();
  7.         form.action = 'demandBids.do?method=view';
  8.         form.target = "main";        
  9.     }
When in the web page itself, and you click the "Get Report" button, it calls the javascript function "getReport()" and changes the action of the form itself from the following:

<action="/emkt/demand/demandBids.do?method=view">

to the following:

<action="/emkt/demand/demandBids.do?method=report">

and brings up a browser "file download" window. I would usually just do a
Expand|Select|Wrap|Line Numbers
  1.  response = br.submit().read() 
to get the xml report file and then write it to a file. I can put in the link to the report directly thru IE7 itself and it will bring up the "file download" window, but if I try to do a br.open() directly on the reports link and try to read it....it returns an empty xml file.

Does anyone have any input on how I can handle this situation??? Is there a way to invoke the javascript from python itself?? Is there a way to change the "input type" of the button to make mechanize see it as clickable??? Should I try to change the action of the form itself??? Any input on this problem would be greatly appreciated. Thank you.
Jan 28 '08 #1
Share this Question
Share on Google+
2 Replies


P: 25
I figured it out. I had to do the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. br.form.action="the wanted action"
  3.  
  4.  
Jan 30 '08 #2

P: 1
thanks, that was exactly what i was looking for :)
Jul 13 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.