ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何使用pythonw运行Selenium Webdriver?

2019-11-21 11:07:40  阅读:282  来源: 互联网

标签:python-2-7 selenium selenium-webdriver windows python


我正在尝试通过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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有