獲取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();
        }
    }
    
    

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM