Controlling a browser using Python, on a Mac

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

提交回复
热议问题