gpt4 book ai didi

python - 如何使用 pythonw 运行 Selenium Webdriver?

转载 作者:可可西里 更新时间:2023-11-01 09:24:25 25 4
gpt4 key购买 nike

我正在尝试通过 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 一起运行?

最佳答案

正如@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 的错误或不兼容。

关于python - 如何使用 pythonw 运行 Selenium Webdriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25203955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com