使用selenium2library设置自动下载首选项配置文件 [英] Set auto download preference profile using selenium2library

查看:232
本文介绍了使用selenium2library设置自动下载首选项配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:robotframework-selenium2library

Environment: robotframework-selenium2library

我正在selenium2library中寻找一种通过在FirefoxProfile中设置首选项来自动下载文件的方法,因为这是我可以找到的解决方案. 但是,似乎我无法使用我在selenium webdriver中列出的方式将首选项配置文件导入selenium2library中的浏览器.

I am looking for a way in selenium2library to make the auto-downloading the file by setting the preference in FirefoxProfile as this is the solution I can find. However, it seems that I can not use the way I listed as following in selenium webdriver to import the preference profile into the browser in selenium2library.

使用Selenium WebDriver:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2) fp.set_preference("browser.download.manager.showWhenStarting",False) fp.set_preference("browser.download.dir",getcwd()) fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream")
browser = webdriver.Firefox(firefox_profile=fp)

Using selenium webdriver:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2) fp.set_preference("browser.download.manager.showWhenStarting",False) fp.set_preference("browser.download.dir",getcwd()) fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream")
browser = webdriver.Firefox(firefox_profile=fp)

我可以在selenium2library中找到open_browser(),但它只占用一个目录,而不是像selenium webdriver这样使用首选项配置文件的灵活性.

I can find the open_browser() in selenium2library but it only eats a directory instead of the flexibility of using the preference profile like selenium webdriver.

Selenium2Library:
open_browser(self, url, browser='firefox', alias=None,remote_url=False, desired_capabilities=None,ff_profile_dir=None)

Selenium2Library:
open_browser(self, url, browser='firefox', alias=None,remote_url=False, desired_capabilities=None,ff_profile_dir=None)

如果我可以像在robotframework-selenium2library中使用硒webdriver一样进行操作,谁能给我一些启发?

我在Github上发现了一个封闭的话题 https://github.com/rtomac/robotframework-selenium2library/issues/18

I found one closed issue talking about this on Github https://github.com/rtomac/robotframework-selenium2library/issues/18

但是,似乎使用配置文件目录而不是灵活地设置firefox配置文件的首选项.

However, it seems to use profile directory instead of having the flexibility to set the preference for firefox profile.

谢谢!

推荐答案

我可以在selenium2library中找到open_browser(),但是它只吃了一个 目录

I can find the open_browser() in selenium2library but it only eats a directory

不.它也吃偏爱.长话是这样的:

No. It also eats preferences. The long story goes like this:

我不太确定您实际上如何使用robotframework-selenium2library.我会说 common 用法是运行导入selenium2library的robotframework测试用例(即简单的UTF-8文本文件).可能的解决方案如下所示(不用说,所有变量都应在*** Variables ***下定义):

I am not quite sure how you actually use robotframework-selenium2library. The common usage, I would say, is to run robotframework test cases (i.e. simple UTF-8 text files) which import selenium2library. A possible solution to your problem would look like this (it goes without saying that all variables should be defined below *** Variables ***):

*** Settings ***
Library           Selenium2Library
Library           Collections

*** Variables ***

*** Test Cases ***
MyTestCase
    ${preferences} =    Create Dictionary   browser.download.folderList  2  browser.download.manager.showWhenStarting  False   # and so on ....
    Open Browser    <yourURL>    desired_capabilities=${preferences}

但是,您的问题表明您打算直接使用selenium2library提供的python函数(例如您在问题中提到的open_browser).在这种情况下,您所需要做的就是在参数desired_capabilities设置正确的情况下调用该函数.

However, your question suggests that you intend to directly use python functions (like open_browser that you mentioned in your question) provided by selenium2library. In that case, all you need to do is to call that function with parameter desired_capabilities set appropriately.

请注意有关该参数的文档( 完整代码可在此处找到):

Please note the documentation on that parameter ( full code is to be found here):

如果您为remote指定一个值,则还可以指定 'desired_capabilities',它是形式为字符串的 key1:val1,key2:val2将用于指定desirable_capabilities 到远程服务器.这对于执行诸如指定 Internet Explorer或指定浏览器和OS的代理服务器(如果有) 您使用的saucelabs.com. 'desired_capabilities'也可以是 字典(使用创建字典"创建)以允许更复杂 配置.

If you specify a value for remote you can also specify 'desired_capabilities' which is a string in the form key1:val1,key2:val2 that will be used to specify desired_capabilities to the remote server. This is useful for doing things like specify a proxy server for internet explorer or for specify browser and os if your using saucelabs.com. 'desired_capabilities' can also be a dictonary (created with 'Create Dictionary') to allow for more complex configurations.

这篇关于使用selenium2library设置自动下载首选项配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆