[英]Selenium firefox webdriver unknown error using GeckoDriver and Python
I've created many test cases using: 我使用以下方法创建了许多测试用例:
This scenario is beeing used by around 50 test cases 该场景被约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
This code simple creates a Firefox web driver, and it is working fine in all the test cases but 1, in one in particular when I execute it in a daily execution i got the following error 这段代码简单地创建了一个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
So, this is breaking a part of selenium web driver, but i have no idea why! 因此,这破坏了硒Web驱动程序的一部分,但是我不知道为什么!
Any ideas what could be going on wrong? 任何想法可能出了什么问题吗?
This looks like you're running into this bug filed on the official Python bug tracker: https://bugs.python.org/issue14847 看起来您好像遇到了此错误,该错误已归档在官方Python Bug跟踪器上: https : //bugs.python.org/issue14847
The comments there suggest that the issue might be related to one of two things: 那里的评论表明该问题可能与两件事之一有关:
Installing packages with distribute
, due to a bug known to exist at least in distribute 0.6.26
. 由于已知至少存在distribute 0.6.26
的错误,因此使用distribute
安装软件包。 If your "daily execution" relies on installing with this bugged version of distribute
, that may explain your issue. 如果你的“每天执行”依赖于与此bug的版本的安装distribute
,这可能说明你的问题。 Do you know if you do this? 你知道你是否这样做吗? And can you update the version of distribute
used in your environment, or perhaps use virtual environments to avoid it? 您是否可以更新您的环境中使用的distribute
版本,或者可以使用虚拟环境来避免这种情况?
Some other way of manipulating sys.modules
such that codecs
is not found and gives None
instead. 操纵sys.modules
其他方法,使得找不到codecs
,而是提供None
。 Does your code do anything like this in some kind of fancy test setup? 您的代码在某种精美的测试设置中是否会执行类似的操作?
Judging by the trace, you've modified your code for brevity, but in a way that might not completely capture the original logic. 从跟踪的角度来看,为了简洁起见,您已经修改了代码,但是可能无法完全捕获原始逻辑。 The UnboundLocalError
suggests that your assignment of driver
was skipped, and you didn't return right away: 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
You can fix UnboundLocalError
pretty easily by doing the return inside the except, or pre-assigning driver
to None
or something, but that won't fix the underlying issue creating the driver. 您可以通过在except内进行返回,或将driver
预先分配为None
或其他方式,很容易地修复UnboundLocalError
,但这不能解决创建驱动程序的根本问题。 That sounds like an issue with your execution environment. 这听起来像您的执行环境有问题。 Maybe something in your run infrastructure is doing something fancy (what are you using as your test runner?) or perhaps there's something corrupt in the Python environment of your daily execution runner. 也许您的运行基础架构中的某件事正在做一些花哨的事情(您用作测试运行者吗?),或者您的日常执行运行者的Python环境中存在某些损坏。 Depending on what the issue is, you might be able to solve the problem by defining a specific virtualenv
for all test execution, so you can be sure it runs the same manually as it does in the automatic daily execution. 根据问题所在,您可以通过为所有测试执行定义特定的virtualenv
来解决问题,因此可以确保它手动运行与每天自动执行相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.