python+selenium 啟動谷歌和火狐下面的flash
1.安裝火狐
selenium 3.7.0
Firefox 54.0
Python 3.6.2
geckodriver-v0.19.1-win64
一、過程中遇到的問題:
1、報錯:selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities
之前用的Firefox 50.0 ,更新瀏覽器至 54.0版本後,此條報錯消失
瀏覽器版本再高的情況下會出現selenium IDE不能錄製
太多問題記不住,只顧著解決問題了,這個是坑了我半天時間
2、報錯:selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
geckodriver未放在環境變數下
搭建過程中Python、Firefox、geckodriver都要新增到環境變數下,可直接把geckodriver放在Python的安裝檔案裡(因為python已經新增到環境變數下)
3、報錯:selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 0
這條同樣是因為瀏覽器版本低,試了firefox53.0報此條錯誤,升級到firefox 54.0後報錯消失
Windows環境下以下載firefox54.0 64位為例:
1、上面的連結開啟,找到下圖的目錄點進去
2、找到下面的目錄點進去
3、頁面最下面有個“Dir zh-CN”,就下載這個安裝程式
4、下載下來直接安裝,並把firefox.exe程式新增到環境變數下
目前最新版本如下:
下載下來之後直接放在Python的安裝檔案裡就可以了(為了geckodriver也在環境變數下)
安裝chrome:
chromedriver的版本需要和本機的chrome瀏覽器對應,才能正常使用;
第一步:下載對應版本的chromedriver驅動檔案,具體版本請對照文章底部的對應關係表:
如本機的chrome瀏覽器版本為:版本 61.0.3163.100(正式版本) (64 位),對應的chromedriver版本為2.33.
第二步:下載後把檔案解壓,然後放到本機chrome瀏覽器檔案路徑裡,如:
C:\Program Files (x86)\Google\Chrome\Application
第三步:操作完後,就可以使用以下程式碼直接調起瀏覽器了:
chromedriver = "C:/你的目錄/chromedriver.exe" os.environ["webdriver.chrome.driver"] = chromedriver browser = webdriver.Chrome(chromedriver) # 設定瀏覽器需要開啟的url browser.get('http://www.baidu.com')
附chromedriver與chrome的對應關係表:
chromedriver版本 | 支援的Chrome版本 |
---|---|
v2.40 | v66-68 |
v2.39 | v66-68 |
v2.38 | v65-67 |
v2.37 | v64-66 |
v2.36 | v63-65 |
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
v2.14 | v39-42 |
v2.13 | v38-41 |
v2.12 | v36-40 |
v2.11 | v36-40 |
v2.10 | v33-36 |
v2.9 | v31-34 |
v2.8 | v30-33 |
v2.7 | v30-33 |
v2.6 | v29-32 |
v2.5 | v29-32 |
v2.4 | v29-32 |
對映錶轉自:
http://blog.csdn.net/huilan_same/article/details/51896672
chrome啟動flash的配置:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeOpitons = Options() prefs= { "profile.managed_default_content_settings.images":1, "profile.content_settings.plugin_whitelist.adobe-flash-player":1, "profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player":1, } chromeOpitons.add_experimental_option('prefs', prefs) driver = webdriver.Chrome('./chromedriver', chrome_options=chromeOpitons)
Firefox啟動flash的配置:
from selenium.webdriver import FirefoxOptions opts = FirefoxOptions() opts.add_argument("--headless") option_profile = webdriver.FirefoxProfile() option_profile.set_preference("plugin.state.flash",2) browser = webdriver.Firefox(firefox_profile=option_profile,options=opts)