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