Controlling a browser using Python, on a Mac

后端 未结 10 2205
小鲜肉
小鲜肉 2021-02-02 02:57

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 includ

相关标签:
10条回答
  • 2021-02-02 03:13

    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.

    0 讨论(0)
  • 2021-02-02 03:14

    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()
    
    0 讨论(0)
  • 2021-02-02 03:16

    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.

    0 讨论(0)
  • 2021-02-02 03:17

    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
    
    0 讨论(0)
  • 2021-02-02 03:19

    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.

    0 讨论(0)
  • 2021-02-02 03:26

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

    0 讨论(0)
提交回复
热议问题