如何使用pythonw运行Selenium Webdriver?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用pythonw运行Selenium Webdriver?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2954字,纯文字阅读大概需要5分钟。
内容图文
我正在尝试通过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的错误或根本不兼容.
内容总结
以上是互联网集市为您收集整理的如何使用pythonw运行Selenium Webdriver?全部内容,希望文章能够帮你解决如何使用pythonw运行Selenium Webdriver?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。