Ubuntu18.04安裝Selenium和環境搭建
1. Python安裝Selenium模組
pip install Selenium
2. 下載瀏覽器驅動
- chrome驅動檔案下載:http://chromedriver.storage.googleapis.com/index.html
- firefox驅動檔案下載: https://github.com/mozilla/geckodriver/releases
- chrome瀏覽器需要下載對應版本的驅動,不然會報錯,找不到一致的版本號就找大版本號一樣的。我這裡就是版本沒對應,所以程式報錯了。
在瀏覽器位址列輸入 chrome://version/
檢視chrome瀏覽器版本號
異常資訊:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 78
3. 解壓驅動檔案
- zip格式可以用unzip命令解壓
unzip chromedriver_linux64.zip
- gz格式用tar命令解壓
tar -zxvf geckodriver-v0.27.0-linux64.tar.gz
- 解壓出來有兩個可執行檔案chromedriver和geckodriver
4. 注意可執行檔名字不能更改,否則會報錯。
4. 環境配置
- 將解壓後的可執行檔案移動到系統環境變數的目錄中,輸入
echo $PATH
檢視系統環境變數。環境變數不一定相同。
- 我這裡是將存放驅動的目錄配置到系統變數中的,我覺得這樣比較方便擴充套件,而且也不會擔心之後忘了放哪裡的。
- 當然也可以選擇移動到系統的環境變數,比如移動到
/usr/local/bin
目錄下。
sudo mv chromedriver /usr/local/bin/
sudo mv geckodriver /usr/local/bin/
5. 驗證環境
- 在終端輸入chromedriver和geckodriver驗證。輸出如圖下就代表環境配置成功了。
- 如下顯示則表示失敗了。
6. pycharm編寫程式碼測試
測試程式碼,正常執行會輸出url地址的html原始碼。
- Firefox
from selenium import webdriver
url = "https://www.baidu.com"
driver = webdriver.Firefox()
driver.get(url)
print(driver.page_source)
driver.close()
- Chrome
from selenium import webdriver
url = "https://www.baidu.com"
driver = webdriver.Chrome()
driver.get(url)
print(driver.page_source)
driver.close()
7. 執行出現異常
- 如果出現以下異常參考這篇部落格解決 以root許可權執行瀏覽器
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process
相關文章
- python+selenium環境搭建,pip安裝selenium失敗Python
- windows環境下安裝seleniumWindows
- zabbix環境安裝搭建
- 在 Ubuntu18.04 下安裝 LNMP 環境UbuntuLNMP
- Ubuntu18.04安裝Python虛擬環境UbuntuPython
- Flutter搭建開發環境和工具安裝配置Flutter開發環境
- Python+Selenium安裝及環境配置Python
- Selenium環境搭建 - Mac電腦Mac
- NodeJS安裝與環境搭建NodeJS
- Ubuntu18.04 搭建go編譯環境UbuntuGo編譯
- Python+Selenium(1)- 環境搭建Python
- ARM環境搭建 — CodeWarrior 的安裝
- Ubuntu18.04 桌面版 搭建 LNMP 環境UbuntuLNMP
- Java安裝和環境配置Java
- 自動化測試 selenium 環境搭建
- mac 搭建python+selenium+chromedriver環境MacPythonChrome
- 【Linux】LNMP 環境搭建(上):安裝篇LinuxLNMP
- [Linux] LNMP 環境搭建(上):安裝篇LinuxLNMP
- 搭建lamp環境以及安裝配置phpmyadminLAMPPHP
- QT Creator配置環境和安裝QT
- MacOS Flutter環境配置和安裝MacFlutter
- JDK環境解析,安裝和目的JDK
- Expo 環境配置和安裝指南
- zookeeper安裝和使用 windows環境Windows
- SSD+HDDwin10環境下安裝ubuntu18.04雙系統Win10Ubuntu
- Linux 下 Go 環境搭建以及 Gin 安裝LinuxGo
- Flutter環境搭建以及開發軟體安裝Flutter
- vue入門(安裝環境與搭建專案)Vue
- PHP視訊處理器安裝環境搭建PHP
- JBOSS安裝配置並搭建本地開發環境開發環境
- Inmp-(2)在ubuntu18.04中搭建lnmp環境UbuntuLNMP
- ubuntu18.04上搭建django專案測試環境UbuntuDjango
- NODEJS環境搭建 第一篇 安裝和部署NODEJSNodeJS
- LNMP 環境安裝LNMP
- golang環境安裝Golang
- swoft環境安裝
- futter環境安裝
- 安裝LNMP環境LNMP