且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个印象深刻的Bug-WebDriverException

更新时间:2022-09-16 23:15:18

还记得19年的适合刚学习爬虫,遇到了一个问题,一直让我印象深刻。回想起来当时环境

selenium==3.0.1

 

此版本执行须要驱动:geckodriver

我先去网址https://github.com/mozilla/geckodriver/releases 这里下载最新的版本。

而后将文件夹解压到C:\geckodriver处,在电脑设置环境变量,以下图所示

一个印象深刻的Bug-WebDriverException

 

而后在建立Firefox的时候设置执行路径

browser = webdriver.Firefox(executable_path = 'C:\geckodriver\geckodriver.exe')

 

Traceback (most recent call last):

  File "C:\learnplace\python_webdevelop_testdriver\functional_tests.py", line 10, in

    browser = webdriver.Firefox()

  File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__

    self.service.start()

  File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start

    os.path.basename(self.path), self.start_error_message)

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

 

Exception AttributeError: "'Service' object has no attribute 'process'" in > ignored

 

出现另一个异常:

Traceback (most recent call last):

  File "C:\learnplace\python_webdevelop_testdriver\functional_tests.py", line 10, in

    browser = webdriver.Firefox(executable_path = 'C:\geckodriver\geckodriver.exe')

  File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__

    keep_alive=True)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__

    self.start_session(desired_capabilities, browser_profile)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session

    response = self.execute(Command.NEW_SESSION, capabilities)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute

    self.error_handler.check_response(response)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

 

我尝试:将binary的浏览器路径添加如下路径

from selenium import webdriverfrom selenium.webdriver.firefox.firefox_binary import FirefoxBinary

# browser = webdriver.Firefox(# firefox_binary='C:\Program Files (x86)\Mozilla Firefox')# browser.get('http://localhost:8000')# assert 'Django' in browser.title

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')

browser = webdriver.Firefox(firefox_binary=binary)

browser.get('http://localhost:8000')

 

然后出现如下异常

Traceback (most recent call last):

  File "C:\learnplace\python_webdevelop_testdriver\functional_tests.py", line 18, in

    browser = webdriver.Firefox(firefox_binary=binary)

  File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__

    keep_alive=True)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__

    self.start_session(desired_capabilities, browser_profile)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session

    response = self.execute(Command.NEW_SESSION, capabilities)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute

    self.error_handler.check_response(response)

  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3

 

由于当时是新手,折腾了好久好久才发现原来是浏览器版本问题!!

以上问题只要更新firefox到47版本就能够了。哎!

时代在变化,现在我用上了今年(2021年)微软开源了一个项目叫「playwright-python」,Playwright 是针对 Python 语言的纯自动化工具,它可以通过单个API自动执行 Chromium,Firefox 和 WebKit 浏览器,同时支持以无头模式、有头模式运行。完美解决安装和其他我遇到的问题,让我更加专注于我的业务代码的开发。

支持浏览器端的录制,生成自动化脚本,支持无头跑脚本

速度快,基本是selenium的好几倍,且支持浏览器异步运行

自动等待API,可拦截请求,随意mock

 

安装步骤:

Pip#

pip install --upgrade pip

pip install playwright

playwright install

 

Conda#

conda config --add channels conda-forge

conda config --add channels microsoft

conda install playwright

playwright install

 

感兴趣的同学,请查看官网

https://playwright.dev/