Selenium+PhantomJS使用时报错原因及解决方案

问题

今天在使用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 '

翻译过来就是:

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

解决方案

selenium版本降级

通过pip show selenium显示,默认安装版本为3.8.1
将其卸载pip uninstall selenium,重新安装并指定版本号pip install selenium==2.48.0
再次运行,发现没有报错,搞定!

使用无界面浏览器
Selenium+Headless Firefox

Selenium+Headless FirefoxSelenium+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()
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()

如上,完美解决~

  • 36
    点赞
  • 119
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
v2.45.0 ======= * Supports native events for Firefox versions 31 (current ESR), and 24 (immediately previous ESR). Native event support has been discontinued for versions of Firefox later than 33. * Removed automatic installation of SafariDriver extention for .NET. From this point forward, users are expected to manually install the SafariDriver extension into their Safari installation in order to drive the browser. This is due to Apple's changes in architecture for Safari extensions. * Added initial implementation of .NET bindings OperaDriver. The .NET bindings will now support the Chromium-based Opera driver without requiring the use of the Java remote WebDriver server. This driver will work with Opera 26 and above, and requires the download of the Opera driver executable. Code cleanup and refactoring will take place under a separate commit. Note that there is still no support in the .NET bindings for the Presto-based Opera without using the remote server, nor is there likely to be. * Added option to not delete anonymous Firefox profile in .NET. This change adds an option to the .NET FirefoxProfile class so that the driver will not delete the anonymous profile created by the driver. Since the driver cannot and should not use an existing profile in situ because of the multiple instance automation case, this change means that modifications applied to the anonymous profile can be retained and used in future anonymous profiles. The implication is that the user can now make modifications to a profile, and retain those profile modifications (e.g., cookies) into other future profiles, simulating persistent changes over multiple browser launches. Fixes issue #7374. * Introduced type safe option in InternetExplorerOptions to set the capability to disable check of mime type of the doucment when setting cookies. When setting cookies, there is a check in the IE driver to validate that the page in the browser is, in fact, an HTML page. Despite the fact that omitting this check can cause unrecoverable crashes in the driver, there is demand for a mechanism to disable this check for older, legacy versions of Internet Explorer. Fixes issue #1227. v2.44.0 ======= * Supports native events for Firefox versions 33 (current), 32 (immediately previous release), 31 (current ESR), and 24 (immediately previous ESR). * Rolled back improper spec compliance for finding elements. * Fixed WebDriverBackedSelenium compatibility with IE5. Fixes issue #7938. v2.43.1 ======= * Point-release to correct version resources in .NET bindings assemblies. No functional changes. v2.43.0 ======= * Supports native events for Firefox versions 32 (current), 31 (immediately previous release and current ESR), and 24 (immediately previous ESR). * Integrated the Microsoft Internet Explorer driver implementation into the .NET bindings. By setting the Implementation property of the .NET bindings' InternetExplorerDriverService class, the user can now force the driver to use the Microsoft implementation. Note that the default is to use the existing open-source implementation based on the Automation Atoms. Over time as the Microsoft implementation matures, this will be switched to use the Microsoft implementation, first by default, then exclusively. To use the Microsoft implementation, the user must have the August 2014 updates to Internet Explorer installed through Windows Update, and must install the IE Web Driver Tool for Internet Explorer 11 download from Microsoft (http://www.microsoft.com/en-us/download/details.aspx?id=44069). * Added safe check for window.localStorage in .NET WebDriverBackedSelenium implementation. Patch provided by Timofey Vasenin. * Implemented pluggable element locator factories for .NET PageFactory. This change allows the user to specify a custom IElementLocatorFactory for locating elements when used with the PageFactory. This gives much more control over the algorithm used to locate elements, and allows the incorporation of things like retries or handling of specific exceptions. * Issue #7367: Set Json.NET to ignore dates when parsing response values. * Issue #7419: Added support for SwitchTo().ParentFrame() in .NET bindings. This brings the .NET bindings into parity with other languages. * Belatedly removed long-obsolete .NET tests for AndroidDriver.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值