【问题标题】:Force selenium to use the portable firefox application强制 selenium 使用便携式 Firefox 应用程序
【发布时间】:2014-04-12 22:54:10
【问题描述】:

我已经安装了 firefox 14 并在机器上安装了 firefox portable 25.0.1 版,我在该机器上运行网站测试。

由于我正在测试的站点的限制,我无法在 Firefox 14 安装上运行我的测试。我也无法升级 Firefox 14 安装。

所以我正在寻找一种解决方案,我可以使用这个便携式 firefox 版本而不是安装的 firefox 14 版本。

我应该如何强制 selenium 使用这个便携版本而不是安装版本?如果有人可以指导我阅读一些描述性文章/博客,那就太好了。

我的代码如下:-

* 变量 *

${SELENIUM_HUB}     remote_url=http://127.0.0.1:4444/wd/hub
${BROWSER}      firefox D:\\Firefox Portable\\FirefoxPortable\\firefox.exe
${CLIENT_URL}       https://abcd.aline.local

Open Browser    ${CLIENT_URL}    ${BROWSER}   ${SELENIUM_HUB}

将路径指定为 D:/Firefox Portable/FirefoxPortable/firefox.exe 不起作用,因为 '/' get 已删除。有什么想法吗?

PS:python是使用的语言

【问题讨论】:

  • 你使用的是 Selenium 还是 Selenium2?

标签: python firefox selenium robotframework


【解决方案1】:

在实例化 Firefox 网络驱动程序时,您可以使用作为 firefox_binary 参数传递的 FirefoxBinary 类指定所需的 Firefox 二进制文件的路径。

http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.firefox_binary.html

http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.webdriver.html#module-selenium.webdriver.firefox.webdriver

确保二进制文件的路径正确,例如:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

firefox_binary = FirefoxBinary("D:\\Firefox Portable\\FirefoxPortable\\firefox.exe")
driver = webdriver.Firefox(firefox_binary=firefox_binary)

使用机器人框架之类的:

${firefox_binary}=  Evaluate    sys.modules['selenium.webdriver.firefox.firefox_binary'].FirefoxBinary("D:\\Firefox Portable\\FirefoxPortable\\firefox.exe")    sys, selenium.webdriver.firefox_binary
Create Webdriver    Firefox    firefox_binary=${firefox_binary}

可能会起作用。

【讨论】:

  • 谢谢。我正在使用机器人框架并将浏览器名称用作:firefox D:\\Firefox Portable\\FirefoxPortable\\firefox.exe 但问题是它给出了一个“值错误”,上面写着“firefox D:\Firefox Portable” \FirefoxPortable\firefox.exe 不是受支持的浏览器。'
  • 我已经更新了一个例子。我认为您的路径可能不正确
  • 谢谢。到目前为止,我已经说明了我的代码(如下)。你能就此提出建议吗?
  • 又更新了,试试看。
【解决方案2】:

Selenium2Library 不允许您在 Open Browser 关键字中指定浏览器路径,但它确实具有有用的 remote_url 参数。在 Selenium2Library 获得适当的 PhantomJS 支持之前,使用 PhantomJS 的方式是通过 remote_url,就像这样 http://spage.fi/phantomjs

所以理论上我们应该能够首先使用便携式 Firefox,方法是启动我们的 Firefox,然后使用remote_url 连接到它。像这样。

Start Process    c:\\path\\to\\portable\\firefox.exe
Open Browser    http://google.com    firefox    main browser    http://localhost:${firefox webdriver port}

问题是我不知道 Firefox 默认使用什么 webdriver 端口或如何指定它。可能还需要为 Firefox 安装 webdriver.xpi 插件。插件可以从这里 C:\Python27\Lib\site-packages\selenium\webdriver\firefox 或者你的 python 安装位置找到。

Selenium2Library 中有一个 Create Webdriver 关键字,它允许我们指定 firefox_binary(以及其他参数)。所以理论上

Create Webdriver    Firefox    firefox_binary=c:\\path\\to\\portable\\firefox.exe

应该可以,但我从中得到的只是“AttributeError: 'str' object has no attribute 'launch_browser'”。

很抱歉,我想不出办法,但通过深入了解 Firefox webdriver 端口或 Create Webdriver 的实际工作原理,您可能会走得更远。

【讨论】:

  • firefix_binary 用于 FirefoxBinary 的实例,而不是路径。 FirefoxBinary 的 init 采用路径。换句话说,你必须构造一个 FirefoxBinary 并将其传入。这就是你得到一个 AttributeError 的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
相关资源
最近更新 更多