How can I run Selenium Webdriver using pythonw?

I am trying to open Firefox browser in Selenium script through a graphical application on Windows. It worked just fine while running from python.exe runw.py

, but when I launch it with pythonw.exe runw.py

, the browser cannot launch. Instead, this throws this exception:

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

      

The problem, of course, is the lack of stdin

or stdout

(I'm not sure) because in this line the ( firefox_binary.py

) does not work :

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)

      

I tried overriding syd.stdout

with the output file before starting the browser, but it didn't work:

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

      

I am running Python2.7 and Selenium 2.40. How can Selenium work with pythonw

?

+3


source to share


2 answers


Like @falsetru, it subprocess

tries to describe the user's file 0

. Calling a subprocess only works if all the descriptors are valid (or all of them None

), and as pythonw

it is a Windows process and it is not, I was forced to subclass to FirefoxBinary

use nul

descriptors:

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)

      

This way I can use my own binary when instantiating the WebDriver:



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

      

This could be a bug or just an incompatibility in Selenium with Python for Windows.

+2


source


Seems like subprocess

try using stdin (file descriptor 0

, not sys.stdin

).

Workaround: Open the file for reading at the beginning of the script (to create file descriptor 0 for the code to use subprocess

).



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

      

+1


source







All Articles