用 Python 的 selenium擴展 驅動 火狐 谷歌 瀏覽器
1、安裝 selenium 擴展
1)、命令 pip install selenium
2)、查看 python selenium版本號
(1)、在cmd窗口中輸入python(按住shift,然後回車,換行編輯):
>>> import selenium
>>> help(selenium)
(2)、直接在cmd中執行 pip show selenium
(3)、我的版本號是 3.141.0
2、安裝谷歌驅動,驅動版本號與谷歌版本號要對應
1)、谷歌驅動下載地址 http://chromedriver.storage.googleapis.com/index.html
3)、驅動方式
(1)、直接把谷歌驅動 chromedriver.exe 放在谷歌瀏覽器安裝目錄下,與 chrome.exe平級(點擊谷歌瀏覽器圖標-右鍵-打開文件所在位置)
比如我的目錄:C:\Users\Administrator\AppData\Local\Google\Chrome\Application 下
驅動代碼:
from selenium import webdriver
#根據自己的實際情況獲取
chrome_driver = "C:/Users/Administrator/AppData\Local/Google/Chrome/Application/chromedriver.exe"
os.environ['webdriver.chrome.driver'] = chrome_driver
driver = webdriver.Chrome(chrome_driver) #打開一個谷歌瀏覽器
(2)、把 chromedriver.exe 放在 python安裝目錄下,與 pythonw.exe 平級。
驅動代碼:
from selenium import webdriver
driver = webdriver.Chrome() # 驅動谷歌
3、安裝火狐驅動(火狐版本與驅動版本的對應關係沒找到,有哪位知道的麻煩留下言,謝謝)
1)、火狐基本信息
火狐版本:選擇幫助-關於firefox,就可以查看到火狐的版本。
火狐瀏覽器全版本:http://ftp.mozilla.org/pub/firefox/releases/
火狐瀏覽器驅動:https://github.com/mozilla/geckodriver/releases
2)、把火狐驅動 geckodriver.exe 放在火狐的安裝目錄下,與 firefox.exe平級(點擊火狐瀏覽器圖標-右鍵-打開文件所在位置)
3)、配置火狐安裝目錄環境變量
我的環境變量路徑爲:C:\Program Files\Mozilla Firefox
4)、python 驅動火狐
from selenium import webdriver
driver = webdriver.Firefox() # 驅動火狐