views:

385

answers:

7

I'm looking for a way to programatically control a browser on a Mac (i.e. not IE) using Python.

The actions I need include following links, checking if elements exist in a page, and submitting forms.

Which solution would you recommend?

Thanks!

+2  A: 

mechanize, if you don't actually need a browser.

Ignacio Vazquez-Abrams
I specifically asked to control a browser for some good reasons.. e.g. the site that I'm automating is AJAX heavy and afaik mechanize and the like are far from even running the most basic DOM-manipulating javascript.
Gj
+2  A: 

Check out: http://code.google.com/p/python-browsercontrol/

Also, you could read this forum page (I know, it's old, but it seems extremely relevant to your question): http://bytes.com/topic/python/answers/45528-python-client-side-browser-script-language

Also: http://docs.python.org/library/webbrowser.html

ErikT
+1  A: 

Several Mac applications can be controlled via OSAScript (a.k.a. AppleScript), which can be sent via the osascript command. O'Reilly has an article on invoking osascript from Python. I can't vouch for it doing exactly what you want, but it's a starting point.

Mike DeSimone
+2  A: 

Might be a bit restrictive, but py-appscript may be the easiest way of controlling a Applescript'able browser from Python.

For more complex things, you can use the PyObjC to achieve pretty much anything - for example, webkit2png is a Python script which uses WebKit to load a page, and save an image of it. You need to have a decent understanding of Objective-C and Cocoa/etc to use it (as it just exposes ObjC objects to Python)

Screen-scaping may achieve what you want with much less complexity.

dbr
+6  A: 

I like Selenium, it's scriptable through Python. The Selenium IDE only runs in Firefox, but Selenium RC supports multiple browsers.

digitaldreamer
[Selenium-RC](http://seleniumhq.org/docs/05_selenium_rc.html) has support for Safari and several other browsers on OS X as well, according to their [web site](http://seleniumhq.org/about/platforms.html#programming-languages).
fiirhok
+2  A: 

Take a look at PyShell (an extension to PyXPCOM):

http://pyxpcomext.mozdev.org/samples.html#pyshell

Inshim
A: 

Check out spynner: http://code.google.com/p/spynner/

Miguel