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

2024-04-20 00:03:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的脚本在Mac和Linux上运行,在cygwin和cygwin Python下出现以下错误:

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存在一个已知的错误/缺乏兴趣。

一个这样的用户更有帮助,并且在这里有一个解决方案:https://stackoverflow.com/a/11104952/1668057

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

我如何实现它并保持代码的可移植性?

(我的Selenium是从PIP安装的,所以比起编辑任何模块文件,我更愿意重写该方法)

编辑:

看到Jeff给出的答案中更像python的方式,我想到了以下方法(注意,我的脚本已经为禁用图像的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

我一点也不熟悉波本,也不知道我希望得到什么样的回报。也就是说,我应该期待像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。。。


Tags: pathinpyselfhomeseleniumlinefirefox
1条回答
网友
1楼 · 发布于 2024-04-20 00:03:07

好吧,有点明显,但是在Win2k8 cygwin上手动设置PATH变量之后,Jeff的答案中的代码工作了,我现在很高兴通过远程Linux在Win2k8机器上运行Firefox。

我没有手动设置路径,认为这是作弊,但即使是这样,也可以作为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)

希望这次旅行能帮助一些人做类似的事情。

相关问题 更多 >