selenium 中啓動firefox瀏覽器的六種方式,所用的是java
1、如果瀏覽器沒有安裝在默認路徑下,則用
System.setProperty("Webdriver.firefox.bin","firefox瀏覽器的安裝路徑");
完整代碼如下:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class KeywordBrowserFirefox {
public static void main(String[] args) {
System.setProperty("Webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
}
}
2、如果firefox瀏覽器安裝在默認路徑下,直接用:
WebDriver driver = new FirefoxDriver();
完整代碼如下:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class KeywordBrowserFirefox {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
}
}
3、用代理方式啓動firefox瀏覽器
有時候某些網站需要用代理模式去訪問,這個時候我們需要設置瀏覽器的代理,並且需要用戶名與密碼才能登陸代理服務器,那麼就需要先獲取到代理服務器的URL,然後輸入用戶名與密碼,例如用代理的模式打開百度頁面,具體在firefox瀏覽器上實現方式爲:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
/*
* firefox代理
*/
public class KeywordBrowserProxy {
public static void main(String[] args) {
FirefoxProfile surrogate = new FirefoxProfile();
// 啓用代理模式
surrogate.setPreference("network.proxy.type", 5);
// 設置代理IP
surrogate.setPreference("network.proxy.http", "IP地址");
// 設置代理端口號
surrogate.setPreference("network.proxy.http_port", 8080);
//啓動firefox瀏覽器,並將surrogate對象傳入
WebDriver driver = new FirefoxDriver(surrogate);
driver.get("http://www.baidu.com");
System.out.println(driver.getCurrentUrl());
//如果需要用戶名與密碼才能登陸代理服務器,先獲取到代理服務器的URL,然後再登陸
driver.get(driver.getCurrentUrl());
driver.manage().window().maximize();
// 先定位到用戶名與密碼輸入框,並輸入用戶名與密碼
WebElement name = driver.findElement(By.id("password_name"));
name.sendKeys("用戶名");
WebElement pw = driver.findElement(By.id("password_pwd"));
pw.sendKeys("密碼");
WebElement button = driver.findElement(By.id("password_submitBtn"));
button.submit();
driver.get("http://www.baidu.com");
}
}
代理模式、IP地址、端口號,可以在firefox瀏覽器中輸入about:config,然後再在搜索欄中輸入network.proxy,如下圖所示:
4、一般由selenium啓動的firefox瀏覽器頁面,是完全乾淨的頁面,如果想啓動firefox的一些配置文件,並且配置文件是本機的, 配置文件的啓動方式請看:firefox瀏覽器查看配置文件的三種方式:https://mp.csdn.net/postedit/81940854,firefox瀏覽器啓動時加載配置文件,代碼如下:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
/*
* firefox啓動時加載本機的配置文件
*/
public class KeywordBrowserProfilesIni {
public static void main(String[] args) {
//創建profilesIni對象
ProfilesIni ini = new ProfilesIni();
//通過名字來獲取到相應的配置文件
FirefoxProfile profile = ini.getProfile("default");
//創建瀏覽器驅動,並將profile傳入,此時啓動時,就會讀取default配置文件來調用firefox瀏覽器了
WebDriver driver = new FirefoxDriver(profile);
driver.get("http:www.baidu.com");
}
}
5、firefox瀏覽器啓動時加載配置文件,如果配置文件不是本機的,是從別的地方拷貝過來的,代碼如下:
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
/*
* firefox瀏覽器啓動時加載配置文件,如果配置文件是從其它地方複製過來的,保存在自己電腦E盤下configurationFiles
*/
public class KeywordBrowserProfilesPath {
public static void main(String[] args) {
//創建文件對象
File file = new File("E:\\configurationFiles");
//把複製過來的文件傳入
FirefoxProfile profile = new FirefoxProfile(file);
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
}
}
6、firefox瀏覽器啓動時,加載某個插件,如:firebug插件,先下載firebug插件,保存在D盤下,代碼如下:
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
/*
* firefox瀏覽器啓動時加載firebug插件,先下載firebug插件,保存在D盤下
*/
public class KeywordBrowserPlug {
public static void main(String[] args) throws IOException {
//firebug插件存放在D盤下
File file = new File("D:\\firebug_1487\\firebug");
//創建profile對象
FirefoxProfile profile = new FirefoxProfile(file);
//調用FirefoxProfile類中addExtension方法
profile.addExtension(file);
//啓動firefox瀏覽器並將profile對象傳入
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
}
}