开始学习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. [置顶] 运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)

    一般的我们喜欢这样对对象赋值: Person p1;Person p2=p1; classT object(another_object), or    A a(b); classT object = ...

  2. JVM优化

    1.堆大小设置 JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64 ...

  3. Entity Framework 配置

    Entity Framework的核心 – EDM(Entity Data Model) EDM概述 实体数据模型,简称EDM,由三个概念组成.概念模型由概念架构定义语言文件 (.csdl)来定义,映 ...

  4. Magento中直接使用SQL语句

    原理: magento是基于Zend Framework的,所以底层用的还是zend的zend db 在文件app/code/core/Mage/Catalog/model/Resource/Eav ...

  5. ASP读取RSS

    <% @language="VBScript"%> <% Function readrss(xmlseed) dim xmlDoc dim http Set ht ...

  6. [jquery]基础篇--this与$this区别

    参考: http://www.cnblogs.com/hannover/p/4109779.html 1.JQuery this和$(this)的区别 相信很多刚接触JQuery的人,很多都会对$(t ...

  7. HDU 3943 K-th Nya Number(数位DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3943 题目大意:求出区间 (P,Q] 中找到第K个满足条件的数,条件是该数包含X个4和Y个7 Samp ...

  8. C语言遍历一个文件夹下面的所有文件

    主要用到的函数/function. These should get you started: opendir() readdir() closedir() fopen() fread() fwrit ...

  9. Trie的C++实现及HDU1251,hdu1671

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  10. java 泛型 窜讲

    一.为什么使用泛型      复用性:泛型的本质就是参数化类型,因而使用编写的泛型代码可以被许多不同类型的对象所复用.      安全性:在对类型Object引用的参数操作时,往往需要进行显式的强制类 ...