开始学习selenium时为了启动Firefox可谓费尽周折,在大神的帮助下才堪堪搞定,走出了selenium的第一步:jdk1.8 + selenium_2.46 + Firefox国际版40.0.3。

1、selenium启动Firefox时,默认启动一个全新的,不加载任何个人数据的浏览器,这也是最简单的:

public void startFirefox(){
driver = new FirefoxDriver();
System.out.println("startFirefox.");
}

当然如果Firefox没有安装在默认路径下这需要我们手动设置Firefox的启动路径:

System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 

WebDriver driver = newFirefoxDriver(); 

2、问题随之而来,如果我们要让浏览器启动时带上我想要的某个扩展,或者是直接按照我的配置文件来启动,我们该怎么做呢?解决方法很简单,也很人性化,那 就是加载配置文件!!首先我们需要new一个Firefox的配置文件对象:FirefoxProfile,然后将我们需要的东西加入到这个文件对象中, 可以是一个插件,也可以是一个已经存在的配置文件:

  FirefoxProfile profile = new FirefoxProfile(); //创建一个Firefox的配置文件的对象
{
//创建需要添加的拓展或是配置文件的对象
//将需要的拓展,已经存在的配置文件等添加到profile中,甚至是直接修改profile中的Firefox的各项参数;
}
FirefoxDriver driver = new FirefoxDriver(profile); //以创建的profile配置文件对象启动Firefox
  1. 启动时加载某个特定的插件,例如Firebug
public void startFirefoxWithPlug(){
File plugFile = new File("file/Firebug_2.0.12.xpi");
FirefoxProfile profile = new FirefoxProfile(); try {
profile.addExtension(plugFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} driver = new FirefoxDriver(profile);
System.out.println("startFirefoxWithPlug.");
}
  1. 启动时加载默认的本地配置文件,加载默认的本地配置文件时,需要首先初始化一个配置文件:default
public void startFirefoxWithProfile(){
ProfilesIni profiles = new ProfilesIni();
FirefoxProfile profile = new FirefoxProfile();
profile = profiles.getProfile("default");
driver = new FirefoxDriver(profile);
System.out.println("startFirefoxWithProfile.");
}
  1. 启动时加载其他的配置文件:
public void startFirefoxWithOtherProfile(){
File profileDir = new File("Profiles/34t8j0sz.default");
FirefoxProfile profile = new FirefoxProfile(profileDir);
driver = new FirefoxDriver(profile);
}
  1. 启动时设置浏览器的参数,以设置代理为例:
public void setProxyOfFirefox(){
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "proxyIp");
profile.setPreference("network.proxy.http_port", "proxyPort");
driver = new FirefoxDriver(profile);
System.out.println("setDownloadDirOfFirefox.");
}

这段代码里的setPreference方法用于设置浏览器的参数,network.proxy.type则是Firefox中的配置代理相对应的字段,这些字段可以在Firefox中的about:config中看到;

其实设置Firefox的启动很简单,只要记住Firefox启动的各种场景都是围绕这配置文件(profile)来设置的,这么一来就不需要去死记那么多的代码,了然于心自然就会了。

selenium各种场景下的启动Firefox的更多相关文章

  1. selenium启动firefox时加载扩展

    有些时候,我们测试需要用到插件或者已经导入的证书(比如金融和安全加密行业),而selenium启动firefox时会打开一个新的,不含有任何插件和个人证书的firefox(等同于全新安装后第一次打开的 ...

  2. selenium+python启动Firefox浏览器失败问题和点击登陆按钮无效问题

    问题1:使用python+selenium编写脚本调用Firefox时报错:

  3. 【Selenium专题】WebDriver启动firefox浏览器

    firefox浏览器不需要下载驱动,原生支持,以下是代码运行环境,firefox启动封装在方法startFirefox()中 import org.openqa.selenium.WebDriver; ...

  4. 7.解决在python中用selenium启动FireFox浏览器启动不了的方法

    首次在利用python中的selenium启动FireFox浏览器时可能碰到如下问题 当输入如下代码时: from selenium import webdriver brower=webdriver ...

  5. Selenium启动Firefox示例(python版)

    目前做selenium自动化使用的主流语言分为java和python,前一篇为java版,本篇介绍python实现selenium启动Firefox. 1 #-*- coding:utf-8 -*- ...

  6. Selenium启动Firefox示例(java版)

    本文示例使用selenium启动Firefox,并将浏览器窗口最大化,在百度搜索框内输入"HelloWorld",最后点击搜索按钮. 源代码如下: 1 package com.se ...

  7. selenium启动firefox、ie、chrome各浏览器方法

    1.启动firefox浏览器 a.如果你的本地firefox是默认路径安装的话,如下方式即可启动浏览器 WebDriver driver = new FirefoxDriver(); driver.g ...

  8. selenium启动Firefox失败

    今天搭建java+selenium环境,搭建几次都失败,总结一下原因 1. selenium启动Firefox,不需要额外的driver 2. Friefox如果没有安装到默认路径C盘,代码中需要修改 ...

  9. selenium启动firefox打开导入向导问题解决

    操作系统:win8-64位 火狐版本:40.0.2 问题描述:selenium启动firefox时,每次启动都提示我导入其他浏览器的页签,如下图所示 解决方法一: 到firefox的profiles. ...

随机推荐

  1. Android 自学之列表选择框Spinner

    列表选择框(Spinner)与Swing编程里面的Spinner不同,这里的Spinner其实就是一个列表选项框. Spinner是ViewGroup的间接子类,因此他也可作为容器使用. Spinne ...

  2. C语言---字符

    1.三元符(三字母词):由三个字符组合起来代表其他字符,三元符可以在没有一些字符时使用 ??( [ ??) ] ??! | ??< { ??> } ??' ^ ??= # ??/ \ ?? ...

  3. win7 X64可用的单文件IE6

    由于日常调试需要用到老版本的ie,没有办法!用ietest老师感觉不好使,动不动就死了.就找到了ie的单文件版,有博主 小明爱切糕制作,IE6.7.8单文件版本 http://www.cnblogs. ...

  4. IP地址,子网掩码划分(转)

    IP地址划分教程 IP和子网掩码我们都知道,IP是由四段数字组成,在此,我们先来了解一下3类常用的IP A类IP段 0.0.0.0 到127.255.255.255 B类IP段 128.0.0.0 到 ...

  5. wsdl 关于nillable和minOccurs 在.NET和java中的不同

    术语约定文章中会反复出现[值类型].[包装类型].[普通引用类型].[元素节点]和[元素取值]的表述1> [值类型]指的是java和.NET中的基本数据类型,如:int:2> [包装类型] ...

  6. 在 Transact-SQL 中使用 TRY...CATCH

    在 Transact-SQL 中使用 TRY...CATCH (注:本文来自于 http://msdn.microsoft.com/zh-cn/library/ms179296.aspx)       ...

  7. css扁平化博客学习总结(一)模块分析

    一.模块分析 1.每开发一个项目之前,首先要对项目进行一个大致规划,它到底要做什么功能,它有什么具体需求. 2.所以需要进行模块化分析,把这些东西具象化,把一个问题模块化,对需求有一个宏观的了解. 3 ...

  8. jQuery 黑白插件

    1 add jQuery and plug in to the page <script src="js/jquery.min.js"></script> ...

  9. Android入门开发之销毁activity

    使用: 销毁.关闭页面activity 如果打开下个页面的同时销毁了本页面,在下个页面无法返回本页面,每次打开APP应用就会先显示一张APP的介绍图.或者LOGO页面,延时几秒进入应用,进入后无法再返 ...

  10. 对list集合中的对象进行排序(转载)

    原文链接:http://blog.csdn.net/veryisjava/article/details/51675036 Collections对List集合中的数据进行排序 有时候需要对集合中的元 ...