新版的 selenium已经放弃PhantomJS改用Chorme headless

新版的 selenium已经放弃PhantomJS改用Chorme headless

 

使用pip show selenium显示默认安装的是3.1.3版本
目前使用新版selenium调用PhantomJS是会报这样的错: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless

如果还想继续用PhantomJS的话只能使用旧版的selenium,卸载之后重新pip install selenium==2.48.0安装成功。
但其实更好的选择,我们可以使用firefox或chrome的headlesss模式,无需重装selenium
只需要添加以下代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')#上面三行代码就是为了将Chrome不弹出界面,实现无界面爬取
browser = webdriver.Chrome(chrome_options=chrome_options)

也能完美实现无界面爬虫

原文地址:https://www.cnblogs.com/it-tsz/p/10593991.html