python selenium+phantomJS自動化測試環境

來源:互聯網
上載者:User

標籤:title   /usr   user   print   配置   amber   col   turned   stat   

0x00配置phantomJS1、 在windows平台下

 

此種方法是彈瀏覽器進行自動化測試的。

1、下載Google的驅動

https://chromedriver.storage.googleapis.com/index.html

2、將解壓後的chromedriver.exe放到chrome瀏覽器的安裝目錄下。

3、在代碼中呼叫瀏覽器驅動,執行自動化操作。

chromedriver = ‘chromedriver絕對路徑‘driver = webdriver.Chrome(chromedriver)driver,get(url)

 

 

 

2、在linux命令列無介面下

 

使用phantomJS配合selenium,可以建立無介面的瀏覽器,這樣即可達到我們的目的。

 

配置方法:

 

1.安裝phantomJS

安裝包:http://phantomjs.org/download.html,包括Windows,Mac OS,Linux版本,自行選擇對應版本下載解壓即可

sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

 

用 tar 解壓 tar.bz2 檔案時可能會遇到這個錯誤

 

debian:/usr/src# tar jxf linux-2.6.26.tar.bz2

tar: bzip2: Cannot exec: No such file or directory

tar: Error is not recoverable: exiting now

tar: Child returned status 2

tar: Error exit delayed from previous errors

 

解決辦法:

此問題時缺少bzip2工具,安裝該工具。

yum install bzip2

 

0x02 安裝selenium

 

pip install selenium

到這裡。環境就配置好了。接下來就可以上代碼了。

 

0x03 範例程式碼

 

from selenium import webdriverdriver = webdriver.PhantomJS(executable_path=‘/bin/phantomjs/bin/phantomjs‘)#這裡的executable_path填你phantomJS的路徑driver.get(‘http://www.xxxx.com‘)print driver.titledriver.quit()

 

可能出現的報錯資訊:

 

selenium.common.exceptions.WebDriverException: Message: ‘phantomjs‘ executable needs to be in PATH

 

這種報錯就是你的PhantomJS儲存路徑填錯了,檢查填寫的路徑是否為phantomjs程式的儲存路徑。

 

注意這裡使用的是windows系統,在路徑前面需要加上r,參考給出的代碼語句。

browser = webdriver.PhantomJS(executable_path=r‘C:\Users\lyh\Anaconda2\phantomjs-2.1.1-windows\bin\phantomjs.exe‘)

 

參考文章

48734389

76622776

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.