开始学习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. MATLAB的循环结构

    循环结构有两种基本形式:while 循环和for 循环.两者之间的最大不同在于代码的重复是如何控制的.在while 循环中,代码的重复的次数是不能确定的,只要满足用户定义的条件,重复就进行下去.相对地 ...

  2. hackerrank Day 10: Binary Numbers

    Task Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 inte ...

  3. C#扫盲之:==/Equals /ReferenceEquals 异同的总结,相等性你真的知道吗?

    1.前言 == Equals ReferenceEquals 三个相等性测试,是.NET提供给程序员使用的三个方法,他们之间有什么联系和区别,你真的仔细研究过?虽然之前也多多少少知道一点,但是有时候又 ...

  4. 使用Javascript获得网页中通过GET方法提交的参数

    下面我将写出一个函数,用来获取GET方法提交的参数 function getParameter(parameterName) { var string = window.location.search ...

  5. 点击 a 标签触发事件而不跳转页面

    有时候需要让 a 标签像 button 一样,被点击的时候触发事件而不跳转页面. <html> <body> <a id="a1" href=&quo ...

  6. ASP.NET Web Froms开发模式中实现程序集的延迟加载

    延迟加载是一个很大的诱惑,可以达到一些比较好的效果,比如: 1.在实体框架中,由于关联数据的数量和使用时机是不确定的,通过延迟加载,仅在使用的时候去执行关联数据的查询操作,减少无谓的数据查询操作,可以 ...

  7. JS 日期工具类-基于yDate

    源码下载 前言:最近在用到JS日期操作时,发现有一些不方便,于是搜素了一些网上的资料,基于一个开源工具类-yDate 进行了个性化定制,封装成了一个日期工具类工具函数大概介绍:1.普通的日期操作2. ...

  8. Android常见开发思路

    开发思路 刷新: 重新获取数据 清空list 更新适配器 关闭进度条. 加载更多 1. 重新获取数据 添加list 更新适配器 添加轮播条. 自己设计轮播条View 引入lib库文件 设置轮播条数据. ...

  9. C++实现元组

    一般我们使用struct时需要在头文件中定义,例如 struct Example { int a; char b; ... }; 这样将数据打包好必须在程序运行之前将其定义好,如果有需求在程序运行时添 ...

  10. 国外一些知名ASP.Net开源CMS系统

    1.Ludico Ludico是C#编写的居于ASP.NET 2.0的Portal/CMS系统.它的模块化设计是你可以按照你希望的使用或开发网站功能.它里面有高级的用户管理,一个所见即所的(WYSIW ...