Controlling a browser using Python, on a Mac

后端 未结 10 2195
小鲜肉
小鲜肉 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:29

    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.

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

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

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

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

    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.

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