知识网2022年03月05日 21:14原创
新版的 selenium已经放弃PhantomJS,所以才会出现module 'selenium.webdriver' has no attribute 'PhantomJS'。
PhantomJS安装都是最新版本,pip install selenium安装的也是最新版本。所以把selenium版本降低。
1.先把selenium卸载,代码如下:
pip uninstall selenium
2.安装selenium==2.48.0版本的,代码如下:
pip install selenium==2.48.0
pip install selenium==3.8.0(这个版本也是支持的)
完美运行成功!!!!!
这里运行webdriver.PhantomJS()的时候还有可能会提示错误:OSError: [Errno 30] Read-only file system: 'ghostdriver.log',点击查看解决办法。
当然还有另一套解决办法,那就用chrome的headless模式,也可以实现同样的效果,selenium官方都不支持phantomjs了,如果不是一定要使用phantomjs才能实现的话,可以考虑chrome的headless模式。
chrome headless的实现代码:
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') driver = webdriver.Chrome(chrome_options=chrome_options)
本文章网址:https://www.shsongjiang.com/p364/
很赞哦!(2)