同chrome、firefox,ie浏览器的启动一样需要driver,但是IEDriver不像chromeDriver一样有多个版本,不同版本对应不同的chrome版本,它只有32位版本和64位版本两种;
下载地址为:https://www.seleniumhq.org/download/
设置驱动代码如下,第二个参数即为驱动的路径,可以任意设定:
System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");
简单启动代码如下,不用带任何设置;
System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
InternetExplorerDriver ie = new InternetExplorerDriver();
ie.get("https://www.baidu.com/");
System.out.println(ie.getTitle());
ie.quit();
使用此种方法使用代码:
public static void main( String[] args )
{
System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
InternetExplorerOptions options = new InternetExplorerOptions();
options.usePerProcessProxy();
InternetExplorerDriver ie = new InternetExplorerDriver(options);
ie.get("https://www.baidu.com/");
System.out.println(ie.getTitle());
ie.quit();
}
添加代理方法和chrome还有firefox相同,如下:
public static void main( String[] args )
{
System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
DesiredCapabilities source = DesiredCapabilities.internetExplorer();
source.setJavascriptEnabled(true);
InternetExplorerOptions options = new InternetExplorerOptions(source);
options.usePerProcessProxy();
Proxy proxy = new Proxy();
proxy.setAutodetect(true);//是否自动检测代理设置;
proxy.setFtpProxy(proxyIpAndPort);//ftp代理地址
proxy.setHttpProxy(proxyIpAndPort);//http代理地址
proxy.setNoProxy(proxyIpAndPort);//无需代理的网址列表,用;分隔
proxy.setProxyAutoconfigUrl(proxyAutoconfigUrl);//代理自动设置地址
proxy.setProxyType(ProxyType.MANUAL);//手工代理,必填
proxy.setSocksPassword(password);//密码
proxy.setSocksUsername(username);//用户名
proxy.setSocksVersion(5);//添加此项会自动设置proxyType为ProxyType.MANUAL,此项设置内容为socks的版本,为4或者5,最新为5
proxy.setSslProxy(proxyIpAndPort);
options.setProxy(proxy);
InternetExplorerDriver ie = new InternetExplorerDriver(options);
ie.get("https://www.baidu.com/");
System.out.println(ie.getTitle());
ie.quit();
}
IE和Chrome还有firefox不同,可以使用的options选项不多,且还可以直接附带Capablitity,如下所示:
public static void main( String[] args )
{
System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
DesiredCapabilities source = DesiredCapabilities.internetExplorer();
source.setJavascriptEnabled(true);
InternetExplorerOptions options = new InternetExplorerOptions(source);
options.usePerProcessProxy();
InternetExplorerDriver ie = new InternetExplorerDriver(options);
ie.get("https://www.baidu.com/");
System.out.println(ie.getTitle());
ie.quit();
}
其他待补充
此方式结果和用Options直接添加Capablities功能相同,使用代码如下:
public static void main( String[] args )
{
System.setProperty("webdriver.ie.driver", "D:\\test\\driver\\IEDriver_64.exe");
DesiredCapabilities source = DesiredCapabilities.internetExplorer();
source.setJavascriptEnabled(true);
InternetExplorerDriver ie = new InternetExplorerDriver(source);
ie.get("https://www.baidu.com/");
System.out.println(ie.getTitle());
ie.quit();
}
添加Capability的方法,即对应key值和value值得方式还未研究,待补充;