1. 程式人生 > >Python3+selenium3測試環境搭建(Mac)

Python3+selenium3測試環境搭建(Mac)

  • 安裝Python3

mac本身自帶Python2.7,但因Python2 版 20年以後不再維護,所以學習Python3 。
在這裡插入圖片描述
Python3下載地址:https://www.python.org/downloads/
Mac版下載安裝後,就可以使用,不需像window去設定環境變數。驗證是否安裝成功,在終端輸入:Python3 回車。出現Python3版本等資訊就說明成功了。
在這裡插入圖片描述

  • 安裝selenium

使用Python整合pip工具安裝;

pip install selenium    # 預設呼叫Python2安裝包,安裝最新版
pip3 install selenium    # pip3 才會呼叫python3的安裝包,安裝最新版
pip3 install selenium==3.11.0    # "==3.11.0"指定安裝的版本

pip3 show selenium    #檢視當前包的版本資訊
pip3 uninstall selenium    #指定版本號安裝 

在這裡插入圖片描述

  • 安裝瀏覽器驅動

Chrome驅動下載地址

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

火狐驅動下載地址

https://github.com/mozilla/geckodriver/releases/

下載完成後,將驅動解壓,拷貝到:usr/local/bin ;(可直接開啟訪達 command+shift+G ,輸入路徑即可)

  • 下載編輯器——sublime Text3

安裝後,

  1. 設定Sublime Text的語法為python
View -> syntax ->python
  1. 新增編譯環境python3.6.5
Tools -> Build System -> New Build System

貼上一下程式碼

{
      "cmd": ["/Library/Frameworks/Python.framework/Versions/3.6/bin/python3", "-u", "$file"],
      "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
      "env": {"PYTHONIOENCODING": "utf8"},
      "selector":
"source.python" } # 第一行是安裝的Python的路徑,檢視Python路徑,終端:which Python3 # 第三行是因為中文會報錯
  1. 設定編譯環境(即上一步儲存的檔案)
Tools -> Build System->選擇檔案
  1. 測試環境是否正確

開啟sublime,輸入Python程式碼,儲存!後,command + B

  • 驗證環境搭建結果

編寫如下指令碼

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("Selenium")
driver.find_element_by_id("su").click()
driver.quit()

Python3+selenium參考1,點選這裡即可到達
Python3+selenium參考2,點選這裡即可到達
sublime 參考3,點選這裡即可到達
參考4,
在這裡插入圖片描述