今天在优化selenium效率时莫名其妙的报错了。
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 '
翻译了一下,意思就是selenium已经放弃Phantomjs,建议使用谷歌或者火狐的无界面浏览器。
网上看了一下,有两种方案。
第一种;
查看当前的selenium版本。 pip show selenium
发现selenium是3.14.1
新版本放弃了phantomjs的无界面浏览器。
所以需要将新版本selenium卸载,pip uninstail selenium
装上2.48.0版本的selenium即可. pip install selenium == 2.48.0
不过2.48.0版本的selenium对python解释器有要求。所以推荐第二种方法。
第二种:
将Chrome或者火狐浏览器转换为无界面的浏览器即可。
代码如下:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def main():
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('log-level=3') # 设置chromedriver的日志级别
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.close()
if __name__ == '__main__':
main()
这种是不是用起来更方便呢。O(∩_∩)O哈哈~