允许Selenium Webdriver在cygwin和标准安装中查找Firefox路径

时间:2013-05-19 17:01:35

标签: python cygwin webdriver

我正在运行一个Win2k8 EC2实例,目的是在* nux框上从Fabric部署后运行一些浏览器自动化任务。

我在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

在cygwin(selenium)下支持Webdriver存在一个已知错误/缺乏兴趣。

SO用户更有帮助,并在此处提供解决方案:https://stackoverflow.com/a/11104952/1668057

这种方法似乎会破坏Mac / * nix下的代码。

如何实现该功能并保持代码可移植?

(我的Selenium是从PIP安装的,所以更愿意覆盖方法而不是编辑任何模块文件)

修改

看到更多pythonic方式建议Jeff的回答,我想出了以下内容(注意我的脚本已经子类化/覆盖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)

在我的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

我对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本地启动Firefox或通过SSH远程启动Firefox ...

1 个答案:

答案 0 :(得分:4)

好吧,有点明显,但是在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)

希望这段旅程能帮助某人做类似的事情。