我对网页进行了一系列测试。我使用Webdriver,我试图检测片刻,例如当浏览器(Firefox)被迫退出gui时。当它发生时,我得到一个非常漫长而丑陋的追溯。
主程序在单独的线程中执行测试套件。例如这段代码:
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
没有捕获任何异常,这是抛出的。我认为第一个例外是相关的,但是如果你需要回溯的第二部分中提到的方法代码,我会添加它。
感谢您的任何建议!
答案 0 :(得分:1)
第一次追溯:
File "c:\Users\david\workspace\tester\sdi\testing.py", line 165, in urlopen
self._driver.get(url);
第二次追溯:
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 168, in urlopen
log.warn("Webdriver crashed or was forced to quit!", file=sys.stderr)
您的try-except
在记录声明方面存在问题。
但是我认为你不能从另一个线程中捕获异常,你必须在它自己的线程中捕获它,例如使用消息队列来通知主线程。