selenium打开firefox/chrome/ie/HtmlUnit四种浏览器的方法
selenium操作浏览器进行自动化测试,首先需要打开一个浏览器,接下来才能对浏览器进行操作。selenium支持firefox/chrome/ie/HtmlUnitDirver四种浏览器,但因为Chrome Driver是Chromium 项目自己支持和维护的,所以需要另外下载安装Chrome Driver,详细介绍见Chrome Driver的wiki 。
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class OpenBrowsers {
public static void main(String[] args) {
//打开默认路径的firefox
WebDriver diver = new FirefoxDriver();
//打开指定路径的firefox,方法1
System.setProperty(“webdriver.firefox.bin”,”D:\\Program Files\\Mozilla Firefox\\firefox.exe”);
WebDriver dr = new FirefoxDriver();
//打开指定路径的firefox,方法2
File pathToFirefoxBinary = new File(“D:\\Program Files\\Mozilla Firefox\\firefox.exe”);
FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
WebDriver driver1 = new FirefoxDriver(firefoxbin,null);
//打开ie
WebDriver ie_driver = new InternetExplorerDriver();
//打开chrome
System.setProperty(“webdriver.chrome.driver”, “D:\\chromedriver.exe”);
System.setProperty(“webdriver.chrome.bin”,”C:\\Documents and Settings\\user\\Local Settings”+”\\Application Data\\Google\\Chrome\\Application\\chrome.exe”);
//打开HtmlUnit浏览器
WebDriverdriver = new HtmlUnitDriver();
发表评论