Selenium+Python自動化測試-安裝下載
Selenium通過使用WebDriver支持市場上所有主流瀏覽器的自動化。
下載安裝
安裝selenium庫
根據合適的編程語言,本文以Python作為編程語言。
Python的Selenium庫的安裝可以使用pip完成:
pip install selenium
下載瀏覽器驅動
selenium支持市面上大多數瀏覽器,比如,Chrome,Firefox,Internet Explorer,Edge,Opera,以及Safari。
這里選用Chrome作為例子。
首先,下載合適的驅動器。驅動器版本需要和瀏覽器的版本保持一致。谷歌瀏覽器的版本可以在“幫助”中查看。下載鏈接可以自行搜索可得。
然后,添加驅動器的安裝路徑到Path路徑中。
在Windows系統中,在“高級設置”->"環境設置"->Path路徑,進行添加即可。
最后,編寫測試腳本,來驗證是否安裝成功。
#Simple assignment
from selenium import webdriver
import time
driver = webdriver.Chrome()
#your code inside this indent
driver.get("http://www.baidu.com")
time.sleep(3)
driver.quit()
預期結果為:成功打開瀏覽器,加載百度后,持續3秒后,關閉瀏覽器。
問題記錄
Q1:自動化測試過程中,出現以下的報錯情況:
USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: 連到系統上的設備沒有發揮作用。 (0x1F)
此問題是由Chrome試圖讀取當前掛起的USB設備的屬性而觸發的。據說下個chrome版本將會解決此問題(使用版本95.0.4638.69)。目前暫時沒有找到有效方法,只能忽略此報錯,實現方法如下:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options=Options()
# 忽略無用的日志
options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
driver=webdriver.Chrome(options=options)
driver.get("http://www.baidu.com")
time.sleep(3)
driver.quit()
