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");
}
}