当浏览器被迫退出时,WebDriver无法捕获异常(Python)



我对网页进行了一系列测试。我为此使用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:Usersdavidworkspacetestersditesting.py", line 165, in urlopen
    self._driver.get(url);
  File "c:Python33libsite-packagesseleniumwebdriverremotewebdriver.py", line 176, in get
    self.execute(Command.GET, {'url': url})
  File "c:Python33libsite-packagesseleniumwebdriverremotewebdriver.py", line 162, in execute
    response = self.command_executor.execute(driver_command, params)
  File "c:Python33libsite-packagesseleniumwebdriverremoteremote_connection.py", line 355, in execute
    return self._request(url, method=command_info[0], data=data)
  File "c:Python33libsite-packagesseleniumwebdriverremoteremote_connection.py", line 402, in _request
    response = opener.open(request)
  File "c:Python33liburllibrequest.py", line 469, in open
    response = self._open(req, data)
  File "c:Python33liburllibrequest.py", line 487, in _open
    '_open', req)
  File "c:Python33liburllibrequest.py", line 447, in _call_chain
    result = func(*args)
  File "c:Python33liburllibrequest.py", line 1268, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "c:Python33liburllibrequest.py", line 1253, in do_open
    r = h.getresponse()
  File "c:Python33libhttpclient.py", line 1143, in getresponse
    response.begin()
  File "c:Python33libhttpclient.py", line 354, in begin
    version, status, reason = self._read_status()
  File "c:Python33libhttpclient.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:Python33libthreading.py", line 637, in _bootstrap_inner
    self.run()
  File "c:Python33libthreading.py", line 594, in run
    self._target(*self._args, **self._kwargs)
  File "c:Usersdavidworkspacetestersditesting.py", line 361, in runFromStart
    self._run()
  File "c:Usersdavidworkspacetestersditesting.py", line 369, in _run
    self.units[0]() # run self.test_openBrowser()
  File "c:Usersdavidworkspacetestersditesting.py", line 418, in test_openBrowser
    result = self.webtester.urlopen(self.url)
  File "c:Usersdavidworkspacetestersditesting.py", line 168, in urlopen
    log.warn("Webdriver crashed or was forced to quit!", file=sys.stderr)
  File "c:Python33liblogging__init__.py", line 1778, in warn
    warning(msg, *args, **kwargs)
  File "c:Python33liblogging__init__.py", line 1773, in warning
    root.warning(msg, *args, **kwargs)
  File "c:Python33liblogging__init__.py", line 1244, in warning
    self._log(WARNING, msg, args, **kwargs)
TypeError: _log() got an unexpected keyword argument 'file'

我不太遵循,为什么try-except不会捕获任何被抛出的例外。我认为第一个例外是相关的,但是如果您需要Trackback第二部分中提到的方法代码,我将添加它。

谢谢您的任何建议!

1st trackback:

File "c:Usersdavidworkspacetestersditesting.py", line 165, in urlopen
   self._driver.get(url);

第二个追溯:

File "c:Usersdavidworkspacetestersditesting.py", line 361, in runFromStart
   self._run()
...
File "c:Usersdavidworkspacetestersditesting.py", line 168, in urlopen
   log.warn("Webdriver crashed or was forced to quit!", file=sys.stderr)

您的try-except在记录语句中有问题。

但是,我认为您无法从另一个线程中捕获一个例外,您必须在自己的线程中捕获它,例如使用消息队列通知主线程。

最新更新