捕获`KeyboardInterrupt`而不关闭Python中的Selenium Webdriver会话

Ele*_*ena 19 python keyboardinterrupt selenium-webdriver

Python程序通过Selenium WebDriver驱动Firefox.代码嵌入在try/ exceptblock中,如下所示:

session = selenium.webdriver.Firefox(firefox_profile)
try:
    # do stuff
except (Exception, KeyboardInterrupt) as exception:
    logging.info("Caught exception.")
    traceback.print_exc(file=sys.stdout)
Run Code Online (Sandbox Code Playgroud)

如果程序因错误而中止,则WebDriver会话不会关闭,因此Firefox窗口保持打开状态.但是如果程序以KeyboardInterrupt异常中止,则Firefox窗口会关闭(我想因为WebDriver会话也被释放了),我想避免这种情况.

我知道两个异常都通过相同的处理程序,因为我"Caught exception"在两种情况下都看到了消息.

我怎么能避免关闭Firefox窗口KeyboardInterrupt

ein*_*ger 5

我有一个解决方案,但它非常难看.

当按下Ctrl + C时,python会收到一个中断信号(SIGINT),它会在整个进程树中传播.Python中还产生一个KeyboardInterrupt,所以你可以尝试处理一些必然的逻辑过程,但被耦合到子进程的逻辑不会受到影响.

要影响将哪些信号传递给子进程,您必须在生成进程之前指定信号的处理方式subprocess.Popen.

有各种选择,这个选项来自另一个答案:

import subprocess
import signal

def preexec_function():
    # Ignore the SIGINT signal by setting the handler to the standard
    # signal handler SIG_IGN.
    signal.signal(signal.SIGINT, signal.SIG_IGN)

my_process = subprocess.Popen(
    ["my_executable"],
    preexec_fn = preexec_function
)
Run Code Online (Sandbox Code Playgroud)

问题是,你不是Popen那个被委托给selenium的人.SO上有各种各样的讨论.从我收集的内容来看,在调用之前没有执行屏蔽时,其他试图影响信号屏蔽的解决方案很容易失败Popen.

另外请记住,在python文档中使用preexec_fn有一个很大的警告,所以请自行决定使用它.

"幸运的是"python允许在运行时覆盖函数,所以我们可以这样做:

>>> import monkey
>>> import selenium.webdriver
>>> selenium.webdriver.common.service.Service.start = monkey.start
>>> ffx = selenium.webdriver.Firefox()
>>> # pressed Ctrl+C, window stays open.
KeyboardInterrupt
>>> ffx.service.assert_process_still_running()
>>> ffx.quit()
>>> ffx.service.assert_process_still_running()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 107, in assert_process_still_running
    return_code = self.process.poll()
AttributeError: 'NoneType' object has no attribute 'poll'
Run Code Online (Sandbox Code Playgroud)

使用monkey.py如下:

import errno
import os
import platform
import subprocess
from subprocess import PIPE
import signal
import time
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common import utils

def preexec_function():
    signal.signal(signal.SIGINT, signal.SIG_IGN)

def start(self):
  """
        Starts the Service.
        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        """
  try:
    cmd = [self.path]
    cmd.extend(self.command_line_args())
    self.process = subprocess.Popen(cmd, env=self.env,
                                    close_fds=platform.system() != 'Windows',
                                    stdout=self.log_file,
                                    stderr=self.log_file,
                                    stdin=PIPE,
                                    preexec_fn=preexec_function)
#                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  except TypeError:
    raise
  except OSError as err:
    if err.errno == errno.ENOENT:
      raise WebDriverException(
        "'%s' executable needs to be in PATH. %s" % (
          os.path.basename(self.path), self.start_error_message)
      )
    elif err.errno == errno.EACCES:
      raise WebDriverException(
        "'%s' executable may have wrong permissions. %s" % (
          os.path.basename(self.path), self.start_error_message)
      )
    else:
      raise
  except Exception as e:
    raise WebDriverException(
      "The executable %s needs to be available in the path. %s\n%s" %
      (os.path.basename(self.path), self.start_error_message, str(e)))
  count = 0
  while True:
    self.assert_process_still_running()
    if self.is_connectable():
      break
    count += 1
    time.sleep(1)
    if count == 30:
      raise WebDriverException("Can not connect to the Service %s" % self.path)
Run Code Online (Sandbox Code Playgroud)

start代码来自selenium,突出显示添加的行.这是一个粗暴的黑客,它可能会咬你.祝你好运:D