Controlling a browser using Python, on a Mac

2020-05-20 02:27发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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:

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

Example:

import re
import mechanize

br = mechanize.Browser()
br.open("http://www.example.com/")
# follow second link with element text matching regular expression
response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1)
assert br.viewing_html()
print br.title()
print response1.geturl()
print response1.info()  # headers
print response1.read()  # body

br.select_form(name="order")
# Browser passes through unknown attributes (including methods)
# to the selected HTMLForm.
br["cheeses"] = ["mozzarella", "caerphilly"]  # (the method here is __setitem__)
# Submit current form.  Browser calls .close() on the current response on
# navigation, so this closes response1
response2 = br.submit()


回答6:

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.



回答7:

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


回答8:

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.



回答9:

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")



回答10:

You can use selenium library for Python, here is a simple example (in form of unittest):

#!/usr/bin/env python3
import unittest
from selenium import webdriver

class FooTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "http://example.com"

    def is_text_present(self, text):
        return str(text) in self.driver.page_source

    def test_example(self):
        self.driver.get(self.base_url + "/")
        self.assertTrue(self.is_text_present("Example"))

if __name__ == '__main__':

    suite = unittest.TestLoader().loadTestsFromTestCase(FooTest)
    result = unittest.TextTestRunner(verbosity=2).run(suite)