views:

94

answers:

3

I am looking for a way to control a browser from Python, i.e. fill out form fields and submit them, possibly call JS functions. I've looked around a bit, but as far as I could see PyWebKitGtk only lets you show the browser as a GUI element, not interface with it.

Is there a way to do this easily? I wrote my program logic in Python, and I would hate to port it to JS. Besides that, even if I'd use pure JS "bookmarklets", those wouldn't be able to read/write to my local filesystem, would they?

Also, some of the content on the page is generated using AJAX, so I'm looking for a solution where javascript runs as normal.

P.S. to quell your suspicions, I'm not trying to automatically fill out forum account creation forms or something similarly spammious, though the task is technically similar. I need to crawl/scrape sites for my research project.

EDIT: IEC looked promising, but I'm working on a Mac.

+1  A: 

Why not just use Python to simulate a browser, parsing the HTML and building the appropriate HTTP requests as necessary?

A quick google yields the following:

Bryan Ross
I'm not simulating the browser because I want the AJAX to run as normal, and simulating all those calls would be a bit tedious.
Noio
+3  A: 

You might try something like Selenium, which is an application to script browsers through various languages including Python - it's meant for cross-browser testing, but might do what you want.

Daniel Roseman
Seconded. Using Selenium IDE, you would open your browser, press record, and then fill out and submit the form yourself. You can then save your recorded session out to python code. Just integrate the selenium code with your current code and you may be golden.
Eric Palakovich Carr
This looks nice. It works the other way around. I'll call python from Selenium, but if that works, things should be fine. Thank you. EDIT: This is great! If that exported python script works it will do _exactly_ what I want.
Noio
A: 

use mechanize module

Lie Ryan