Python uses SELENIUM/PHANTOMJS

Source: Internet
Author: User

Install Selenium:

1 pip install selenium

Install PHANTOMJS:

1234 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2tarjxvf phantomjs-1.9.7-linux-x86_64.tar.bz2cp phantomjs-1.9.7-linux-x86_64/bin/phantomjs /bin/chmod755 /bin/phantomjs

Examples of Use:

12345 fromselenium import webdriverdriver = webdriver.PhantomJS()driver.get("http://www.baidu.com")data = driver.titleprintdata

Via Remote Selenium Server:

12345678910111213 from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdriver = webdriver.Remote(  command_executor=‘http://192.168.1.3:4444/wd/hub‘,  desired_capabilities={‘browserName‘: ‘PhantomJS‘,                                  ‘version‘: ‘2‘,                                  ‘javascriptEnabled‘: True})driver = webdriver.Remote(   command_executor=‘http://192.168.1.3:4444/wd/hub‘,   desired_capabilities=DesiredCapabilities.PHANTOMJS)driver.get("http://www.baidu.com")data = driver.titleprint data

Phantomjs and Firefox speed comparison:

1234567891011121314151617181920212223242526 import unittestfrom selenium import webdriverimport timeclass TestThree(unittest.TestCase):    def setUp(self):        self.startTime = time.time()    def test_url_fire(self):        self.driver = webdriver.Firefox()        self.driver.get("http://www.qq.com")        self.driver.quit()    def test_url_phantom(self):        self.driver = webdriver.PhantomJS()        self.driver.get("http://www.qq.com")        self.driver.quit()    def tearDown(self):        t = time.time() - self.startTime        print "%s: %.3f" % (self.id(), t)        self.driver.quitif __name__ == ‘__main__‘:    suite = unittest.TestLoader().loadTestsFromTestCase(TestThree)    unittest.TextTestRunner(verbosity=0).run(suite)

Python uses SELENIUM/PHANTOMJS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.