0
我有一系列的网页测试。为此,我使用Webdriver,并尝试检测一会儿,例如,当浏览器(Firefox)被迫在gui中退出时。当它发生时,我会得到一个非常漫长而又丑陋的回溯。浏览器强制退出时Webdriver无法捕捉异常(python)
主程序在单独的线程中执行测试套件。例如此代码:
def urlopen(self, url):
''' Opens browser driver and redirects it to specified url addresss. It web driver
is not initialized, it tries to initialize it at first.
'''
# check webdriver initialization, if broken or not initialized, can be fixed
try:
self.redirectToBlank(self.driver);
except (urllib.error.URLError, AttributeError): # User closed web driver or is None
try:
self.initDriver()
except:
raise
# !! this is the moment, when I close the browser window
# if there is a problem with URL loading, it cannot be reapaired
try:
self._driver.get(url);
except:
print("Webdriver crashed or was forced to quit!", file=sys.stderr)
此方法用于打开浏览器。 initDriver
方法来初始化self._driver
,这是webdriver.Firefox
Exception in thread Thread-2:
Traceback (most recent call last):
File "c:\Users\david\workspace\tester\sdi\testing.py", line 165, in urlopen
self._driver.get(url);
File "c:\Python33\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 176, in get
self.execute(Command.GET, {'url': url})
File "c:\Python33\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 162, in execute
response = self.command_executor.execute(driver_command, params)
File "c:\Python33\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 355, in execute
return self._request(url, method=command_info[0], data=data)
File "c:\Python33\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 402, in _request
response = opener.open(request)
File "c:\Python33\lib\urllib\request.py", line 469, in open
response = self._open(req, data)
File "c:\Python33\lib\urllib\request.py", line 487, in _open
'_open', req)
File "c:\Python33\lib\urllib\request.py", line 447, in _call_chain
result = func(*args)
File "c:\Python33\lib\urllib\request.py", line 1268, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "c:\Python33\lib\urllib\request.py", line 1253, in do_open
r = h.getresponse()
File "c:\Python33\lib\http\client.py", line 1143, in getresponse
response.begin()
File "c:\Python33\lib\http\client.py", line 354, in begin
version, status, reason = self._read_status()
File "c:\Python33\lib\http\client.py", line 324, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: ''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Python33\lib\threading.py", line 637, in _bootstrap_inner
self.run()
File "c:\Python33\lib\threading.py", line 594, in run
self._target(*self._args, **self._kwargs)
File "c:\Users\david\workspace\tester\sdi\testing.py", line 361, in runFromStart
self._run()
File "c:\Users\david\workspace\tester\sdi\testing.py", line 369, in _run
self.units[0]() # run self.test_openBrowser()
File "c:\Users\david\workspace\tester\sdi\testing.py", line 418, in test_openBrowser
result = self.webtester.urlopen(self.url)
File "c:\Users\david\workspace\tester\sdi\testing.py", line 168, in urlopen
log.warn("Webdriver crashed or was forced to quit!", file=sys.stderr)
File "c:\Python33\lib\logging\__init__.py", line 1778, in warn
warning(msg, *args, **kwargs)
File "c:\Python33\lib\logging\__init__.py", line 1773, in warning
root.warning(msg, *args, **kwargs)
File "c:\Python33\lib\logging\__init__.py", line 1244, in warning
self._log(WARNING, msg, args, **kwargs)
TypeError: _log() got an unexpected keyword argument 'file'
一个实例我不太明白,为什么try-except
没有发现任何异常,这被抛出。我认为第一个异常是相关的,但是如果您需要回溯的第二部分中提到的方法代码,我会添加它。
谢谢你的任何建议!
您好,先生,值得一枚奖牌。 try-except块只处理发生在同一个线程中的异常。然后为了与主线程进行通信,我使用了一个队列。这个问题,我错过了,是我使用队列进行同步,但在这种情况下,我从两个线程写入日志文件,这就是问题所在。再次感谢让我意识到! – dakov