点击打开链接https://www.cnblogs.com/yajing-zh/p/4897728.html
一、SELENIUM2启动浏览器
注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理的模式。
1.启动firefox浏览器:建议使用,要不有可能会出问题。
启动不在默认安装路径的firefox浏览器:
2.启动chrome浏览器:
如果不想用setProperty的方式,也可以将chromedriver.exe 放在”C:\Windows\System32”路径下并重启电脑即可
3.启动IE浏览器:
需要IEDriverServer.exe 的支持,且 IE的exe文件分64位与32位,请根据自已的机器选择相应的exe文件。
启动代码如下:
二、SELENIUM2启动浏览器时加载插件
加载插件的作用:有的是为了调试,也有的是被测试的系统本身需要插件
Firefox下加载firebug插件:
Chrome下加载crx插件:
且IE下没有插件加载。
三、SELENIUM2启动firefox时的profile设置
在firefox地址栏中输入about:config,可以看到有哪些可以设置
举例:导出文件的地址设置
3.启用默认情况下被firefox禁用的功能,以本地事件例,很简单直接设置为true就可以了。
特别重要:启动时,每次都是一个干净的firefox,如果要启动本机器的firefox的配置:
如果在机器B上要启动机器A上的firefox配置,则先导出A的配置,然后加载:
将A机器上的Profiles文件夹”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”给拷贝出来
四、SELENIUM2启动chrome时的profile设置
特别重要:将user data文件夹” C:\Users\cloudchen\AppData\Local\Google\Chrome\User Data”拷贝出来
请自己验证一下,加不加以下代码的区别:
五、SELENIUM2启动IE时的设置
特别重要:启动IE时,需关闭保护模式。 4个红框标记的区域中的保护模式的checkbox 都要uncheck
也可以代码关闭:
完整代码如下:
package com.selenium.test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class launchBrowser {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
launchChrome();
}
public static void launchFireFox() {
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference(
"browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", "d:\\test");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv");
String proxyIp = "child-prc.intel.com";
int proxyPort = 913;
firefoxProfile.setPreference("network.proxy.type", 1);
firefoxProfile.setPreference("network.proxy.http", proxyIp);
firefoxProfile.setPreference("network.proxy.http_port", proxyPort);
firefoxProfile.setPreference("network.proxy.ssl", proxyIp);
firefoxProfile.setPreference("network.proxy.ssl_port", proxyPort);
firefoxProfile
.setPreference("network.proxy.share_proxy_settings", true);
firefoxProfile
.setPreference("network.proxy.no_proxies_on", "localhost");
WebDriver driver = new FirefoxDriver(firefoxProfile);
// WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("https://baidu.com");
// driver.close();
// driver = null;
}
public static void launchChrome() {
System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
WebDriver driver = new ChromeDriver();
Navigation navigation = driver.navigate();
navigation.to("https://www.baidu.com");
driver.close();
driver = null;
}
public static void launchIE() {
System.setProperty("webdriver.ie.driver", "files/iedriver64.exe");
WebDriver driver = new InternetExplorerDriver();
Navigation navigation = driver.navigate();
navigation.to("https://www.baidu.com");
driver.close();
driver = null;
}
}