如何使用pythonw运行Selenium Webdriver?
我正在尝试通过Windows中的GUI应用程序以Selenium脚本打开Firefox浏览器。当使用如何使用pythonw运行Selenium Webdriver?,python,windows,python-2.7,selenium,selenium-webdriver,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 "bi
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\u binary.py
)失败:
在运行浏览器之前,我已尝试使用输出文件覆盖syd.stdout,但无效:
sys.stdout = sys.stderr = open('log.txt', 'a+')
我正在运行Python2.7和Selenium 2.40。Selenium如何在
pythonw
下运行?似乎是子流程
尝试使用stdin(文件描述符0
,而不是sys.stdin
)
解决方法:在脚本开头打开一个文件以进行读取(使文件描述符为0,它将由子流程使用)
正如@falsetru所说,子流程
正在尝试使用文件描述符0
。如果所有句柄都有效(或者如果它们都是None
),并且pythonw
是一个Windows进程,并且没有任何句柄,那么子进程调用将只起作用,我被迫子类化FirefoxBinary
以使用nul
句柄:
类WindowsFirefoxBinary(FirefoxBinary):
定义从配置文件开始路径(自我,路径):
self.\u firefox\u env[“XRE\u PROFILE\u PATH”]=路径
如果platform.system().lower()=“linux”:
self.\u修改\u链接\u库\u路径()
命令=[self.\u start\u cmd,“-silent”]
如果self.command_行不是None:
对于self.command_行中的cli:
命令追加(cli)
#添加了stdin参数:
nul=打开(os.devnull,“w+”)
Popen(命令,stdin=num,stdout=self.\u log\u文件或num,stderr=stdout,
env=self.\u firefox\u env.communicate()
命令[1]=“前台”
self.process=Popen(
命令,stdin=num,stdout=self.\u log\u file或num,stderr=stdout,
env=self.\u firefox\u env)
这样,我可以在创建WebDriver实例时使用自己的二进制文件:
binary = WindowsFirefoxBinary()
browser = webdriver.Firefox(firefox_binary=binary)
这可能是一个错误,也可能是Selenium与Python for Windows不兼容。您是否尝试过让STDOUT
引用sys.STDOUT的新值?
import os
open(os.devnull, 'r')
binary = WindowsFirefoxBinary()
browser = webdriver.Firefox(firefox_binary=binary)