同事寫了支 Java 程式會用到 Firefox 抓網站的螢幕擷圖,不過今天執行起來怪怪的,我仔細想一下,好像是我把 Firefox 更新後發生的問題?看了一下錯誤訊息,裡面有這一段內容:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
hello\AppData\Local\Temp\anonymous1263966490208696868webdriver-profile\extensions\fxdriver@googlecode.com
1461490691668   addons.xpi      DEBUG   Ignoring file entry whose name is not a valid add-on ID: C:\Users\hello\AppData\Local\Temp\anonymous1263966490208696868webdriver-profile\extensions\webdriver-staging
1461490691668   addons.xpi      INFO    SystemAddonInstallLocation directory ismissing
1461490691669   addons.xpi      INFO    Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to C:\Program Files (x86)\Mozilla Firefox\browser\extensions\{972ce4c6-7e
08-4474-a285-3208198ce6fd}
1461490691670   addons.xpi      DEBUG   Skipping unavailable install location app-system-share

試著用程式呼叫的 Firefox 連到 http://127.0.0.1:7055,會看到一個很素的畫面,裡頭只有兩行字:

httpd.js
If you're seeing this page, httpd.js is up and serving requests! Now set a base path and serve some files!

看起來是一個小型的 Web Server,不過我很肯定跑程式的機器沒有裝任何 Web Server,於是猜測可能是 Firefox plugin / extension,果然在「附加元件→擴充套件」看到一個叫做 Firefox WebDriver 的 extention:

firefox WebDriver被Firefox停用的畫面

(發生錯誤時,這個是被 Firefox 強制停用的。)

 

原本以為是同事在 Firefox 手動安裝的套件,所以試著按照《解決 Firefox 升級後,反而不能使用擴充套件的問題》,自己到 Firefox 的網址列輸入 about:config,找到 xpinstall.signatures.required,快點兩下把它的設定值從 true 設成 false

奇怪的是這個設定值卻存不起來。如果是我自己去程式集裡把 Firefox 打開來,xpinstall.signatures.required 的值的確變成 false。但是我們的 Java 程式每次把 Firefox 啟動起來,我去看 about:config,裡頭的 xpinstall.signatures.required 仍然是 true。

 

後來再查了一下發現 WebDriver 是我們用來撈資料的 Selenium 裡頭的一個套件,所以把方向改成調整程式。按照 Github 上頭 "Firefox 43 disable Firefox Web-driver #1385" 這篇文章裡的建議,修改程式裡建立 FirefoxProfile 物件時的設定:

final FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("xpinstall.signatures.required", false); // 主要是要加這行!
driver = new FirefoxDriver(firefoxProfile);

加完以後再重新包程式,執行起來就沒有問題了。

 

下次不要手賤亂更新 Firefox 了。Orz Orz Orz

arrow
arrow
    文章標籤
    selenium firefox
    全站熱搜

    小攻城師 發表在 痞客邦 留言(0) 人氣()