如何使用pythonw运行Selenium Webdriver?

时间:2014-08-08 12:49:24

标签: python windows python-2.7 selenium 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

问题当然在于没有stdinstdout(我不确定),因为它在这一行(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一起运行?

2 个答案:

答案 0 :(得分:2)

就像@falsetru所说,subprocess正在尝试用户文件描述符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与Python for Windows的不兼容。

答案 1 :(得分:1)

似乎subprocess尝试使用stdin(文件描述符0,而不是sys.stdin)。

解决方法:在脚本开头打开读取文件(使subprocess代码使用的文件描述符为0)。

import os
open(os.devnull, 'r')