使用GeckoDriver和Python的Selenium firefox webdriver未知错误

时间:2017-10-19 18:12:45

标签: python selenium firefox webdriver geckodriver

我使用以下方法创建了许多测试用例:

  • Firefox v55
  • Python 2.7.3
  • GeckoDriver v0.19.0

大约50个测试用例使用了这个场景

try:
    caps = DesiredCapabilities.FIREFOX
    caps["wires"] = True
    driver = webdriver.Firefox(capabilities=caps)
    driver.maximize_window()

    self.log("Selenium driver created for %s" % browser)
    return self.STATUS.PASS, driver

except Exception, e:
    self.log("Error when creating selenium driver")
    self.log(traceback.format_exc())
    return self.STATUS.FAIL 

这段代码很简单,创建了一个Firefox Web驱动程序,它在所有测试用例中运行良好但是1,特别是当我在日常执行中执行它时,我得到了以下错误

Traceback (most recent call last):

File "C:\QA Tools\selenium\CreateSeleniumDriver.py", line 26, in run

driver = webdriver.Firefox(capabilities=caps)

File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 154, in __init__
keep_alive=True)

File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 140, in __init__
self.start_session(desired_capabilities, browser_profile)

File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)

File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 295, in execute
response = self.command_executor.execute(driver_command, params)

File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 464, in execute
return self._request(command_info[0], url, body=data)

File "c:\python27\lib\site-packages\selenium-3.5.0-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 538, in _request
body = data.decode('utf-8').replace('\x00', '').strip()

File "c:\python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)

AttributeError: 'NoneType' object has no attribute 'utf_8_decode'

Selenium driver created for Firefox
Error when creating selenium driver
Traceback (most recent call last):
File "C:\QA Tools\selenium\CreateSeleniumDriver.py", line 63, in run
return self.STATUS.PASS, driver

UnboundLocalError: local variable 'driver' referenced before assignment

所以,这打破了selenium web驱动程序的一部分,但我不明白为什么!

  • 我已经尝试了一切让它发挥作用
  • 这适用于我使用它的所有其他场景
  • 这只发生在Firefox
  • 如果它是手动执行的,但是在执行所有其他测试用例的日常执行时不能正常工作(检查任何与此类执行相关的内容并且没有发现问题)

任何可能出错的想法?​​

1 个答案:

答案 0 :(得分:0)

看起来你正在遇到官方Python bug追踪器上提到的这个错误:https://bugs.python.org/issue14847

那里的评论表明这个问题可能与以下两个方面有关:

  1. 安装包含distribute的软件包,因为已知错误至少存在于distribute 0.6.26中。如果您每天执行"依赖于使用此distribute的错误版本进行安装,这可能会解释您的问题。你知道吗?您是否可以更新环境中使用的distribute版本,或者使用虚拟环境来避免它?

  2. 其他一些操纵sys.modules的方式,导致找不到codecs,而是提供None。您的代码是否在某种奇特的测试设置中执行此类操作?

  3. 根据跟踪判断,为了简洁起见,您修改了代码,但这种方式可能无法完全捕获原始逻辑。 UnboundLocalError表示您的driver作业已被跳过,而且您没有立即回复:

    try:
        caps = DesiredCapabilities.FIREFOX
        caps["wires"] = True
        # This line raises an AttributeError deep inside a standard python lib:
        driver = webdriver.Firefox(capabilities=caps)
        driver.maximize_window()
    
        self.log("Selenium driver created for %s" % browser)
    
    except Exception, e:
        # The driver variable was never actually created or assigned due to the
        # AttributeError above.
        self.log("Error when creating selenium driver")
        self.log(traceback.format_exc())
    
    # Some omitted code...
    
    # The driver variable does not exist past the try if the exception happened
    # before assignment to driver. So this raises the UnboundLocalError:
    return self.STATUS.PASS, driver
    

    您可以通过在except中进行返回,或者将UnboundLocalError预分配到driver或其他内容来轻松修复None,但这不会解决根本问题创建驱动程序。这听起来像是执行环境的问题。您的运行基础架构中的某些东西可能正在做一些花哨的东西(您使用什么作为您的测试运行器?)或者在您的日常执行运行程序的Python环境中可能存在某些损坏的东西。根据问题的原因,您可以通过为所有测试执行定义特定的virtualenv来解决问题,因此您可以确保它与自动每日执行中的手动运行相同。