最近报了炼数成金的selenium学习班,将每节课的笔记记录下来,以作备用。在此感谢张飞的课程设计及指导。
本篇涉及chrome、firefox、IE浏览器的打开
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SeleniumDemo {
//打开firefox,并跳转百度首页
public static void myFirefox() {
WebDriver wd = new FirefoxDriver(); //创建firefox浏览器对象
Navigation navigation = wd.navigate(); //创建导航
navigation.to("http://www.baidu.com"); //跳转到百度
wd.close(); //关闭浏览器
wd = null; //释放wd
}
//打开chrome,并跳转百度首页
public static void myChrome(){
System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
WebDriver wd = new ChromeDriver();
Navigation navigation = wd.navigate(); //创建导航
navigation.to("http://www.baidu.com"); //跳转到百度
wd.close(); //关闭浏览器
wd = null; //释放wd
}
//打开ie,并跳转到百度首页
public static void myIe(){
System.setProperty("webdriver.ie.driver", "files/IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver wd = new InternetExplorerDriver(ieCapabilities);
Navigation navigation = wd.navigate();
navigation.to("http://www.baidu.com");
wd.close();
wd = null;
}
//启动firefox时,加载firebug插件
public static void myFirefoxpro(){
File file = new File("g:/firebug-2.0.7-fx.xpi");
FirefoxProfile firefoxprofile = new FirefoxProfile();
try{
firefoxprofile.addExtension(file);
}
catch(IOException e){
e.printStackTrace();
}
firefoxprofile.setPreference("extensions.firebug.currentVersion", "2.0.7");//设置firebug版本
WebDriver wd = new FirefoxDriver(firefoxprofile);
}
//设置firefox自动下载目录
public static void myFirefoxPreference(){
FirefoxProfile firefoxprofile = new FirefoxProfile();
//设置成0代表下载到浏览器默认下载路径;设置成2则可以保存到指定目录;设置成1保存到桌面
firefoxprofile.setPreference("browser.download.folderList",2);
//是否显示开始
firefoxprofile.setPreference("browser.download.manager.showWhenStarting",false);
//指定下载路径
firefoxprofile.setPreference("browser.download.dir", "C:\\Temp");
//不询问下载路径;后面的参数为要下载页面的Content-type的值
firefoxprofile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/otet-stream,text/csv,application/vnd.ms-excel,application/zip");
WebDriver wd = new FirefoxDriver(firefoxprofile);
Navigation navigation = wd.navigate();
navigation.to("http://www.baidu.com");
}
//设置启动Chrome并自动启mini-notepad插件
public static void myChromePro(){
System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
ChromeOptions option = new ChromeOptions();
option.addExtensions(new File("G:/Mini-Notepad_v5.0.7.crx"));
WebDriver wd = new ChromeDriver(option);
}
//启动本机的firefox设置
public static void myFirefoxDefault(){
ProfilesIni allprofile = new ProfilesIni();
FirefoxProfile profile = allprofile.getProfile("default");
WebDriver wd = new FirefoxDriver(profile);
wd.get("http://www.baidu.com");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// myFirefox();
// myChrome();
// myIe();
// myFirefoxPreference();
// myChromePro();
myFirefoxDefault();
}
}
炼数成金的优惠码:T75X,用此优惠码可减免50%的固定学费。