1. 程式人生 > >Selenium3.14.1+Python安裝和第一個Demo

Selenium3.14.1+Python安裝和第一個Demo

言簡意賅的說下Selenium是什麼

Selenium是前臺測試框架,支援IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome等瀏覽器,我只試了這四個,別的大家用的時候可以試下。

用指令碼可以模擬網頁的點選,輸入,提交,驗證等操作。

可參照下列網站進行學習:

1.https://selenium-python-zh.readthedocs.io/en/latest/installation.html(中文)

2.https://selenium-python.readthedocs.io/index.html(英文)

安裝/執行Demo:

1.Python官方網站下載最新版Python

   https://www.python.org/downloads/

2.通過pip下載安裝Selenium

  也可以直接進入Python的Scripts目錄,按住Shift+滑鼠右鍵,選擇PowerShell視窗。

  注意有提示升級Pip的時候根據命令升級你的PIP

D:\>cd D:\python\Scripts

D:\python\Scripts>pip install selenium

 3.檢視Selenium版本

D:\python\Scripts>pip show selenium
Name: selenium
Version: 3.14.1
Summary: Python bindings 
for Selenium Home-page: https://github.com/SeleniumHQ/selenium/ Author: UNKNOWN Author-email: UNKNOWN License: Apache 2.0 Location: d:\python\lib\site-packages Requires: urllib3 Required-by: D:\python\Scripts>

   注意版本,下載完成後將geckodriver.exe放到Python的安裝目錄下。我的是D:\Python,然後將D:\Python新增到系統的環境變數中

  

5.編寫指令碼

from selenium import webdriverfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Firefox()driver.get("http://www.python.org")assert "Python" in driver.titleprint (driver.title)elem = driver.find_element_by_name("q")elem.clear()elem.send_keys("pycon")elem.send_keys(Keys.RETURN)assert "No results found." not in driver.page_sourcedriver.close()

6.執行Demo

D:\python>python 目錄\testPython.py
Welcome to Python.org

D:\python>

已上只是跑一個簡單的Demo,大家也可以下載Selenium的IDE進行工作更方便。