Python使用Selenium/PhantomJS

來源:互聯網
上載者:User

標籤:

安裝selenium:

1 pip install selenium

安裝PhantomJS:

1234 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2tar jxvf phantomjs-1.9.7-linux-x86_64.tar.bz2cp phantomjs-1.9.7-linux-x86_64/bin/phantomjs /bin/chmod 755 /bin/phantomjs

使用樣本:

12345 from selenium import webdriverdriver = webdriver.PhantomJS()driver.get("http://www.baidu.com")data = driver.titleprint data

通過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和Firefox速度對比:

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.quit if __name__ == ‘__main__‘:    suite = unittest.TestLoader().loadTestsFromTestCase(TestThree)    unittest.TextTestRunner(verbosity=0).run(suite)

Python使用Selenium/PhantomJS

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.