• python报错系列(14)--Selenium support for PhantomJS has been deprecated


    系列文章目录


    前言

    一、Selenium support for PhantomJS has been deprecated

    E:\py\python3.7\Python37\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: 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 '
    
    • 1
    • 2

    翻译:用户警告:Selenium对PhantomJS的支持已被弃用,请使用无头版本的Chrome或Firefox代替
    警告。“Selenium对PhantomJS的支持已被弃用,请使用headless”

    二、原因分析

    1.selenium已经放弃PhantomJS,了,建议使用火狐或者谷歌无界面浏览器

    PS E:\py\python3.7\test2\test43> python3 -m pip show selenium
    WARNING: Ignoring invalid distribution -ip (e:\py\python3.7\python37\lib\site-packages)
    Name: selenium                                                                                                              nlmZwRtFAwvDqyjOWdztaPdjM-givOWr5tL8Y8cO71pvZnaQa09ueKNNzeKhzVvuSUm_l2xhhA_FfOm8cfTeE26tGtIozKPVfiWbkc4PxKBRLa9qx8CKGFjLeGZQtdjGuBoE2lUr-Version: 3.141.0
    Summary: Python bindings for Selenium
    Home-page: https://github.com/SeleniumHQ/selenium/                                                                          hantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
    Author: UNKNOWN
    Author-email: UNKNOWN
    License: Apache 2.0
    Location: e:\py\python3.7\python37\lib\site-packages
    Requires: urllib3
    Required-by:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.解决方案

    1、selenium版本降级
    2、通过python3 -m pip uninstall selenium卸载,重新安装指定版本python3 -m pip install selenium==2.48.0
    3、再次运行

    3.参考

    解决方案

    1. selenium版本降级
    2. 通过pip show selenium显示,默认安装版本为3.8.1。
    3. 将其卸载pip uninstall selenium,重新安装并指定版本号pip install selenium==2.48.0。
    4. 再次运行,发现没有报错,搞定!
    5. 使用无界面浏览器
      Selenium+Headless Firefox
      Selenium+Headless Firefox和Selenium+Firefox,区别就是实例option的时候设置-headless参数。

    前提条件:

    • 本地安装Firefox浏览器
    • 本地需要geckodriver驱动器文件,如果不配置环境变量的话,需要手动指定executable_path参数。

    示例代码:

    from selenium.webdriver import Firefox
    from selenium.webdriver.firefox.options import Options
    
    
    def main():
        options = Options()
        options.add_argument('-headless')
        driver = Firefox(executable_path='./geckodriver', firefox_options=options)
        driver.get("https://www.qiushibaike.com/8hr/page/1/")
        print(driver.page_source)
        driver.close()
    
    
    if __name__ == '__main__':
        main()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Selenium+Headless Chrome

    与Firefox类似

    前提条件:

    • 本地安装Chrome浏览器
    • 本地需要chromedriver驱动器文件,如果不配置环境变量的话,需要手动指定executable_path参数。

    示例:

    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')
        driver = webdriver.Chrome(executable_path='./chromedriver', chrome_options=chrome_options)
        driver.get("https://www.baidu.com")
        print(driver.page_source)
        driver.close()
    
    
    if __name__ == '__main__':
        main()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    总结

    分享:
    成大事者大都具有容人所不容、忍人所不忍的心胸,善于求大同存小异,团结大多数人。他们不斤斤计较,不纠缠于非原则性的琐事,所以才能成大事,立伟业。

  • 相关阅读:
    IP可视对讲实时录制系统
    python-自动化篇-运维-网络-IP
    Python之第六章 内置容器 --- 集合
    H5的基础
    redis 学习记录
    将二维整型数组以二进制输出到文件与二进制文件读入到数组
    ECMAScrip-ES6-新变量-let--注意块级作用域
    从new File("")到jdk源码
    2022年最新辽宁水利水电施工安全员考试题库及答案
    机器人中的数值优化|【七】线性搜索牛顿共轭梯度法、可信域牛顿共轭梯度法
  • 原文地址:https://blog.csdn.net/qq_45365214/article/details/125426363