Controlling a browser using Python, on a Mac

2020-05-20 02:25发布

I'm looking for a way to programatically control a browser on a Mac (i.e. Firefox or Safari or Chrome/-ium or Opera, but 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?

10条回答
手持菜刀,她持情操
2楼-- · 2020-05-20 02:47

Checkout Mozmill https://github.com/mikeal/mozmill

Mozmill is a UI Automation framework for Mozilla apps like Firefox and Thunderbird. It's both an addon and a Python command-line tool. The addon provides an IDE for writing and running the JavaScript tests and the Python package provides a mechanism for running the tests from the command line as well as providing a way to test restarting the application.

查看更多
再贱就再见
3楼-- · 2020-05-20 02:50

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

Example:

promptSvc = components.classes["@mozilla.org/embedcomp/prompt-service;1"].\
        getService(Components.interfaces.nsIPromptService)
promptSvc.alert(None, 'Greeting...', "Hello from Python")

Python PyShell 0.1, Mozilla, popup, OK

查看更多
beautiful°
4楼-- · 2020-05-20 02:51

Check out spynner Python module.

Spynner is a stateful programmatic web browser module for Python. It is based upon PyQT and WebKit. It supports Javascript, AJAX, and every other technology that !WebKit is able to handle (Flash, SVG, ...). Spynner takes advantage of JQuery. a powerful Javascript library that makes the interaction with pages and event simulation really easy.

Using Spynner you would able to simulate a web browser with no GUI (though a browsing window can be opened for debugging purposes), so it may be used to implement crawlers or acceptance testing tools.

See some examples at GitHub page.

查看更多
劫难
5楼-- · 2020-05-20 02:52

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

查看更多
放我归山
6楼-- · 2020-05-20 02:53

Check out 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

Example:

from browser import *
my_browser = Firefox(99, '/usr/lib/firefox/firefox-bin') my_browser.open_url('cnn.com')

open_url returns when the cnn.com home page document is loaded in the browser frame.

查看更多
兄弟一词,经得起流年.
7楼-- · 2020-05-20 03:01

Maybe overpowered, but check out Marionette to control Firefox. There is a tutorial at readthedocs:

You first start a Marionette-enabled firefox instance:

firefox -marionette

Then you create a client:

client = Marionette('localhost', port=2828)
client.start_session()

Navigation f.ex. is done via

url = 'http://mozilla.org'
client.navigate(url)
client.go_back()
client.go_forward()
assert client.get_url() == url
查看更多
登录 后发表回答