编程语言
首页 > 编程语言> > 如何使用pythonw运行Selenium Webdriver?

如何使用pythonw运行Selenium Webdriver?

作者:互联网

我正在尝试通过Windows中的GUI应用程序以Selenium脚本打开Firefox浏览器.与python.exe runw.py一起运行时,它工作得很好,但是当我与pythonw.exe runw.py一起运行时,浏览器无法启动.相反,它引发了这个异常:

Traceback (most recent call last):
  File "bin\runw.py", line 215, in process_instance
    instance.setup()
  File "bin\mixin.py", line 181, in setup
    self.browser = self.get_firefox_browser()
  File "bin\mixin.py", line 166, in get_firefox_browser
    firefox_binary=binary, firefox_profile=profile)
  File "C:\myvirtualenv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "C:\myvirtualenv\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "C:\myvirtualenv\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 60, in launch_browser
    self._start_from_profile_path(self.profile.path)
  File "C:\myvirtualenv\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 83, in _start_from_profile_path
    env=self._firefox_env).communicate()
  File "C:\Python27\Lib\subprocess.py", line 702, in __init__
    errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
  File "C:\Python27\Lib\subprocess.py", line 833, in _get_handles
    p2cread = self._make_inheritable(p2cread)
  File "C:\Python27\Lib\subprocess.py", line 884, in _make_inheritable
    _subprocess.DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid

问题当然在于没有stdin或stdout(我不确定),因为它在这一行中失败(firefox_binary.py):

def _start_from_profile_path(self, path):
    ...
    Popen(command, stdout=self._log_file, stderr=STDOUT,
          env=self._firefox_env).communicate()
    command[1] = '-foreground'
    self.process = Popen(
        command, stdout=self._log_file, stderr=STDOUT,
        env=self._firefox_env)

在运行浏览器之前,我尝试用输出文件覆盖syd.stdout,但是它不起作用:

sys.stdout = sys.stderr = open('log.txt', 'a+')

我正在运行Python2.7和Selenium 2.40. Selenium如何与pythonw一起运行?

解决方法:

就像@falsetru所说的那样,子进程正在尝试使用文件描述符0.只有所有句柄都有效(或者它们全部为None)时,子进程调用才能工作,并且pythonw是Windows进程并且没有任何进程,所以我强制子类FirefoxBinary使用nul句柄:

class WindowsFirefoxBinary(FirefoxBinary):

    def _start_from_profile_path(self, path):
        self._firefox_env["XRE_PROFILE_PATH"] = path

        if platform.system().lower() == 'linux':
            self._modify_link_library_path()
        command = [self._start_cmd, "-silent"]
        if self.command_line is not None:
            for cli in self.command_line:
                command.append(cli)

        # Added stdin argument:
        nul = open(os.devnull, 'w+')
        Popen(command, stdin=nul, stdout=self._log_file or nul, stderr=STDOUT,
              env=self._firefox_env).communicate()
        command[1] = '-foreground'
        self.process = Popen(
            command, stdin=nul, stdout=self._log_file or nul, stderr=STDOUT,
            env=self._firefox_env)

这样,我可以在创建WebDriver实例时使用自己的二进制文件:

binary = WindowsFirefoxBinary()
browser = webdriver.Firefox(firefox_binary=binary)

这可能是Selenium与Windows版Python的错误或根本不兼容.

标签:python-2-7,selenium,selenium-webdriver,windows,python
来源: https://codeday.me/bug/20191121/2051463.html