ljs*_*dev 5 python cygwin webdriver
我正在运行 Win2k8 EC2 实例,目的是在从 Fabric 部署到 *nux 框后运行一些浏览器自动化任务。
我在 Mac 和 Linux 上运行的脚本在使用 cygwin Python 的 cygwin 下出现以下错误:
File "/home/Myuser/.virtualenvs/myproject/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 141, in _get_firefox_start_cmd
" Please specify the firefox binary location or install firefox")
RuntimeError: Could not find firefox in your system PATH. Please specify the firefox binary location or install firefox
Run Code Online (Sandbox Code Playgroud)
在 cygwin ( selenium )下支持 Webdriver 有一个已知的错误/缺乏兴趣。
一位 SO 用户更有帮助,并在此处提供解决方案:https : //stackoverflow.com/a/11104952/1668057
该方法似乎会破坏我在 Mac/*nix 下的代码。
我该如何实现并保持代码的可移植性?
(我的 Selenium 是从 PIP 安装的,所以宁愿覆盖该方法而不是编辑任何模块文件)
编辑:
看到杰夫的回答建议的更多 pythonic 方式,我想出了以下内容(注意我的脚本已经子类化/覆盖了用于禁用图像的 FirefoxProfile 类):
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from subprocess import Popen, PIPE
class CygwinFirefoxProfile(FirefoxProfile):
@property
def path(self):
path = self.profile_dir
try:
proc = Popen(['cygpath','-d',path], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
path = stdout.split('\n', 1)[0]
print("cygwin path found")
except OSError:
print("No cygwin path found")
return path
class CarServiceOnlineBookingsTest(unittest.TestCase):
def setUp(self):
firefoxProfile = CygwinFirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
self.driver = webdriver.Firefox(firefoxProfile)
Run Code Online (Sandbox Code Playgroud)
在我的 Mac 上,这现在捕获异常并继续正常运行,但是在检测到 cygwin 路径的 Win2k8 机器上,它仍然失败并出现以下错误:
Traceback (most recent call last):
File "myscript.py", line 45, in setUp
self.driver = webdriver.Firefox(firefoxProfile)
File "/home/Myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 50, in __init__
self.binary = FirefoxBinary()
File "/home/Myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 33, in __init__
self._start_cmd = self._get_firefox_start_cmd()
File "/home/Myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 141, in _get_firefox_start_cmd
" Please specify the firefox binary location or install firefox")
RuntimeError: Could not find firefox in your system PATH. Please specify the firefox binary location or install firefox
Run Code Online (Sandbox Code Playgroud)
我一点也不熟悉 Popen 或者我期望得到积极结果的回报。即,我应该期待类似的东西C:\Program Files (x86)\Firefox\Firefox.exe吗?
下一步调试在哪里?
编辑#2:
从 cygwin bash shell 执行此命令会打开 Firefox:
/cygdrive/c/Program\ Files\ \(x86\)/Mozilla\ Firefox/firefox.exe
我想我的下一步是将其硬编码到脚本中,看看它是否允许 Selenium 通过 cygwin bash 本地或通过 SSH 远程启动 Firefox ...
好吧,有点明显,但是在 Win2k8 cygwin 上手动设置 PATH 变量后,Jeff 答案中的代码可以工作,我现在很高兴通过远程 Linux 机器在 Win2k8 机器上运行 Firefox。
我没有手动设置 PATH,认为这是作弊,但如果我想要完全自动化,即使这也可以作为 Fabric 脚本的一部分来完成......
下面是现在在 Mac 和 Windows 上都可以正常运行的代码:
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from subprocess import Popen, PIPE
class CygwinFirefoxProfile(FirefoxProfile):
@property
def path(self):
path = self.profile_dir
# cygwin requires to manually specify Firefox path a below:
# PATH=/cygdrive/c/Program\ Files\ \(x86\)/Mozilla\ Firefox/:$PATH
try:
proc = Popen(['cygpath','-d',path], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
path = stdout.split('\n', 1)[0]
except OSError:
print("No cygwin path found")
return path
class CarServiceOnlineBookingsTest(unittest.TestCase):
def setUp(self):
firefoxProfile = CygwinFirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
self.driver = webdriver.Firefox(firefoxProfile)
Run Code Online (Sandbox Code Playgroud)
希望这段旅程能帮助那些做类似事情的人。