获取FirefoxProfile配置文件以及使用方法介绍

使用默认方式构建的(WebDriver)FirefoxDriver实例:

WebDriver driver = new FirefoxDriver();

这种方式下,打开的Firefox浏览器将是不带任何插件的浏览器,和初始安装一样的状态。有时在测试中需要使用到预先保留的一些信息,比如Cookie中的用户名和密码等,显然这种方式不适用了。

 

这里可以采用下面的方式来构建(WebDriver)FirefoxDriver实例:

String firefoxProfileDir = "C:\\Users\\XXXXX\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\a6xwo0b1.default";

FirefoxProfile profile = new FirefoxProfile(new File(firefoxProfileDir));

WebDriver driver = new FirefoxDriver(profile);

 

这里firefoxProfileDir的获取方式是:

通过在开始菜单中的“搜索程序和文件”中输入%APPDATA%\Mozilla\Firefox\Profiles\ 来获取路径

详细可以参考

http://www.tuicool.com/articles/NJv6Nj

https://support.mozilla.org/zh-CN/kb/用户配置文件

 

实例:

package com.test.mouse;

import java.io.File;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class MouseOperation {

    public static void main(String[] args) {
        
        //通过加载配置文件使得由WebDriver启动的firefox浏览器也能共享之前安装过的插件以及保存的密码等信息
        FirefoxProfile profile = new FirefoxProfile(new File("C:\\Users\\huangch\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\a6xwo0b1.default"));        
        WebDriver driver = new FirefoxDriver(profile);
        
        driver.get("http://c37.yunpan.360.cn");
        driver.manage().window().maximize();
        waitTime(5000);
        
        driver.findElement(By.xpath("//*[@id='infoPanel']/a[2]")).click();
        
//可以使用下面的方法将当前页面对应的Cookies内容打印出来
        Set<Cookie> setCookie = driver.manage().getCookies();
        Iterator iterator = setCookie.iterator();
        while(iterator.hasNext()){
            Cookie c = (Cookie) iterator.next();
            System.out.println(c.getDomain()+"---"+c.getName()+"---"+c.getPath()+"---"+c.getValue());
        }


    }
    
    static public void waitTime(int time) {

        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    

}

 

转载于:https://www.cnblogs.com/xinxin1994/p/7289609.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值