2024年6月29日 星期六

Selenium 4 不再支援 PhantomJS

我在手上幾本網路爬蟲的書裏面 (都是 2018 年買的) 看到 PhantomJS 的介紹, 趁著離六月底還有兩天想來試試看這個無頭模式運作的模擬瀏覽器怎麼用, 沒想到下載好 Windows 版的 PhantomJS 放到 Python 安裝目錄後執行 PhantomJS() 卻出現如下錯誤 :

Python 3.10.11 (C:\Users\tony1\AppData\Local\Programs\Thonny\python.exe)
>>> from selenium import webdriver   
>>> driver=webdriver.PhantomJS()    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'selenium.webdriver' has no attribute 'PhantomJS'

搜尋此錯誤訊息發現, 原來由於 PhantomJS 已在 2018 年停止開發, 所以 Selenium 也已從第 4 版起停止對 PhantomJS 的支援了, 參考 :


"The native support for Opera and PhantomJS is removed in Selenium 4, as their WebDriver implementations are no longer under development. The Opera browser is based on Chromium, and users looking to test their implementation on Opera can opt for testing on the Chrome browser."

如果想使用 PhantomJS 可用虛擬環境安裝 Selenium 3 (例如 Selenium 3.3.0) 或以下之版本來跑 : 

pip install selenium==3.3.0

哈哈, 我總是做甚麼都慢半拍, 連 Selenium 的書都是買來 4 年才看, 難怪要用都過時了. 雖然如此我還是稍微查詢了一下 PhantomJS 的歷史以資緬懷先烈的偉業 : 

PhontomJS 於 2011 年由開發者 Ariya Hidayat 發布, 可在 Windows, MacOS, 與 Linux 下運作. 不過由於乏人投入後續開發, PhantomJS 已於 2018 年停止維護, 其最後版本為 v2.2.1, 參考 :


PhantomJS 原始碼寄存於 GitHub :


教學文件參考 :


如果還在用 Selenium 3 的話倒是可以試看看 PhantomJS, 但我可沒那麼多閒功夫. 

沒有留言 :