Selenium2——profile设置、启动Firefox浏览器

自学Selenium2 ( WebDriver ),理论和实践的差距还是很大的,所以学习任何编程语言、工具,实践是最好的老师。

进入正题,这篇文章讲述,在自动化测试时,对Firefox浏览器的profile设置、启动有所不同,需根据自己情况进行相应修改。


1 Firefox的profie设置

自动化测试时,有可能会遇到下载文件的情况,如下图;目前Selenium2还无法处理这样的对话框,但可通过对Firefox的profile预先进行设置达到自动下载的效果。


1.1 创建FirefoxProfile对象

FirefoxProfile profile = new FirefoxProfile();

1.2 设置下载路径

// 设置是否询问下载位置(可忽略);默认true——不询问,直接下载到指定路径,具体设置见browser.folderList,false——询问
profile.setPreference("browser.download.useDownloadDir",true);
// 指定下载位置
profile.setPreference("browser.download.downloadDir", "c:\\OutputFolder");
// 设置下载方式;0——下载到桌面,默认1——下载到Firefox默认位置,2——下载到指定位置
profile.setPreference("browser.download.folderList", 2);
// 当一个下载开始时,设置是否显示下载管理器;默认true——显示,flase——不显示
profile.setPreference("browser.download.manager.showWhenStarting",false);


2 设置无需确认即可下载的文件格式

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/xml");

注意:

MIME类型是设置某种拓展名的文件用一种应用程序打开的方式类型。

常见文件的MIME类型:

.txttext/plain 或 text/x-log
.pdfapplication/pdf
.csvtext/csv
.xlsapplication/vnd.ms-excel
.docapplication/msword
.zipapplication/zip
.xmlapplication/xml
.rarapplication/octet-stream
.exeapplication/octet-stream
.gifimage/gif
.jpegimage/jpeg
.htmltext/html

有时,需要的文件无法在搜索引擎上查询到其对应文件类型的MIME类型,可在浏览器中查看,如Firefox浏览器的工具栏 -> 选项 -> 应用程序。


3 启动Firefox时加载插件

WebDriver启动的是一个干净的没有任务、插件、cookies信息的Firefox浏览器(即使本机Firefox安装某些插件),但在自动化测试中可能需要插件(如Firebug)进行调试。

注意:需要下载firebug.xpi,且最好使用非Firefox浏览器下载,不然提示直接安装到Firefox;最好不要在Firebug官网中下载,因为提示你需要使用Firefox浏览器。

// 定义插件所在位置
File file = new File("E:\\Firefox\\files\\firebug-2.0.17.xpi");
// 创建一个FirefoxProfile对象profile
FirefoxProfile profile = new FirefoxProfile();
try{
// 将Firebug加载到profile对象中
profile.addExtension(file);
}catch (IOException e){
e.printStackTrace();
}
// 设置Firebug的当前版本号
profile.setPreference("extensions.firebug.currentVersion","2.0.17");
// 激活Firebug
profile.setPreference("extensions.firebug.allPagesActivation","on");


4 启动Firefox浏览器

4.1 Firefox安装在默认路径下

直接创建一个FirefoxDriver对象。

WebDriver driver = new FirefoxDriver();

4.2 Firefox未安装在默认路径下

需要指定Firefox的可执行程序firefox.exe的路径,再创建FirefoxDriver对象。

System.setProperty("webdriver.firefox.bin", "E:\\Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();


结合起来,就是如下代码:

File file = new File("E:\\Firefox\\files\\firebug-2.0.17.xpi");
FirefoxProfile profile = new FirefoxProfile();
try{
	profile.addExtension(file);
} catch (IOException e){
	e.printStackTrace();
}
profile.setPreference("extensions.firebug.currentVersion","2.0.17");
profile.setPreference("extensions.firebug.allPagesActivation","on");
profile.setPreference("browser.download.downloadDir","C:\\Output");
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/xml");
System.setPreperty("webdriver.firefox.bin","E:\\Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: selenium是一个自动化测试工具,它可以模拟用户操作浏览器,执行各种自动化测试任务。如果需要接管已打开的Firefox浏览器,可以通过以下步骤来实现: 1. 安装selenium:首先,您需要在计算机上安装selenium。您可以从selenium官方网站上找到安装指南,并按照指南进行安装。 2. 下载geckodriver:geckodriver是一个用于Firefox浏览器的驱动程序,它允许seleniumFirefox浏览器进行通信。您需要下载与您的Firefox浏览器版本相对应的geckodriver,并将其解压到一个易于访问的位置。 3. 在代码中指定geckodriver路径:在使用selenium时,您需要在代码中指定geckodriver的路径。您可以使用以下代码来设置geckodriver路径: ```python from selenium import webdriver # 设置geckodriver路径 path_to_geckodriver = 'geckodriver的路径' # 启动Firefox浏览器并指定geckodriver路径 browser = webdriver.Firefox(executable_path=path_to_geckodriver) ``` 4. 接管Firefox浏览器:通过以上步骤,您已经成功地启动了一个新的Firefox浏览器实例。要接管已经打开的Firefox浏览器,您需要使用Firefox浏览器的会话ID。您可以使用以下代码来接管已打开的Firefox浏览器: ```python from selenium import webdriver # 设置geckodriver路径 path_to_geckodriver = 'geckodriver的路径' # 设置已打开的Firefox浏览器的会话ID session_id = '已打开的Firefox浏览器的会话ID' # 启动与会话ID相对应的Firefox浏览器并指定geckodriver路径 browser = webdriver.Remote(command_executor=f'http://127.0.0.1:4444/session/{session_id}', desired_capabilities={'browserName': 'firefox'}, options=options) ``` 通过以上步骤,您可以使用selenium来接管已打开的Firefox浏览器,并执行各种自动化测试任务。请确保您已正确设置geckodriver的路径,并提供正确的会话ID以接管已打开的Firefox浏览器。 ### 回答2: Selenium是一种流行的自动化测试工具,它可以用于驱动各种浏览器执行操作。要接管已打开的Firefox浏览器,我们需要使用Selenium的远程WebDriver来连接到已打开的浏览器实例。 首先,我们需要确保已经安装了Selenium WebDriver的Firefox驱动程序。然后,通过创建一个FirefoxDriver实例,我们可以打开一个新的Firefox浏览器窗口。 要接管已打开的浏览器,我们需要获取该浏览器的会话ID。我们可以通过执行以下代码来获取会话ID: ``` String sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); ``` 然后,我们可以使用获取的会话ID来创建一个新的FirefoxDriver实例,并将会话ID传递给它: ``` FirefoxOptions options = new FirefoxOptions().setLegacy(true); FirefoxDriver takenOverDriver = new FirefoxDriver(options); takenOverDriver = new FirefoxDriver(new HttpCommandExecutor(new URL("http://localhost:4444/"), sessionId, null), options); ``` 这将创建一个新的FirefoxDriver实例并将其连接到已打开的Firefox浏览器窗口。 现在,我们可以使用新的FirefoxDriver实例来执行各种操作,例如导航到不同的URL、查找元素、填写表单等。 值得注意的是,接管已打开的浏览器可能需要一些额外的配置,例如网络代理设置浏览器配置文件等,以确保正确地管理已打开的浏览器实例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值