Python使用Selenium/PhantomJS

轉(zhuǎn)載一篇文章,為Selenium/PhantomJS做準(zhǔn)備

  • PhantomJS 無瀏覽器操作
  • Selenium啟動(dòng)瀏覽器操作

phantomjs使用

from selenium import webdriver
  driver = webdriver.PhantomJS()
  driver.get("http://www.baidu.com")
  data = driver.title
  print data

senelium 簡(jiǎn)單使用

Remote Selenium Server

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = 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.title
print data

PhantomJS和Firefox速度對(duì)比:

import unittest
from selenium import webdriver
import time
class 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)

來自:http://www.cnblogs.com/skying555/p/4494057.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容