一,准备文件 :
准备两个包在eclipse中files和lib
Lib下面
log4j-1.2.17.jar
selenium-server-standalone-2.44.0.jar
files文件:
二,代码
代码:
package webdriver;
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.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class day001 {
public static void main(String[] args) {
mychrome();
myfirefox();
myie();
}
//火狐浏览器
public static void myfirefox(){
WebDriver driver=new FirefoxDriver();
Navigation Navigation=driver.navigate();
Navigation.to("http://nkweb-uat.huawei.com/tkb");
//driver.navigate().to("http://nkweb-uat.huawei.com/tkb");
// driver.get("http://nkweb-uat.huawei.com/tkb");
//driver.close();
// driver=null;
}
//谷歌浏览器
public static void mychrome(){
System.setProperty("webdriver.chrome.driver","files/chromedriver.exe");
WebDriver driver=new ChromeDriver();
Navigation Navigation=driver.navigate();
Navigation.to("http://nkweb-uat.huawei.com/tkb");
//driver.get("http://nkweb-uat.huawei.com/tkb");
}
//IE浏览器
public static void myie(){
System.setProperty("webdriver.ie.driver","files/IEDriverServer.exe");
//设置关闭模式
DesiredCapabilities dcb = DesiredCapabilities.internetExplorer();
dcb.setCapability("InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS",true);
dcb.setCapability("ignoreProtectedModeSettings", true);
WebDriver driver = new InternetExplorerDriver(dcb);
//WebDriver driver=new InternetExplorerDriver();
Navigation Navigation=driver.navigate();
Navigation.to("http://nkweb-uat.huawei.com/tkb");
//driver.get("http://nkweb-uat.huawei.com/tkb");
}
}
三,问题及解决方法
操作期间注意:
1, 报错:会报错谷歌必须大于39版本的
解决方法:那么需要升级版本大于39版本的谷歌浏览器
2. 问题:Navigation可能联想不到方法
解决方法:主要是对象首写字母没有大写而导致的
3.报错 Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
解决方法:public static void StratIe(){ System.setProperty("webdriver.ie.driver", "files/IEDriver.exe"); DesiredCapabilities dcb = DesiredCapabilities.internetExplorer(); //关闭保护模式 dcb.setCapability("InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS",true); dcb.setCapability("ignoreProtectedModeSettings", true); WebDriver driver = new InternetExplorerDriver(dcb);
4.报错
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Browser zoom level was set to 119%. It should be set to 100% (WARNING: The server did not provide any stacktrace information)
由于浏览器比例问题导致webDriver启动IE失败 现像:IE能启动但时提示“This is the initial start page for the WebDriver server.”
解决:将IE浏览器缩放比例设置成100%。之后再运行就成功了。
JavaScript warning: resource://addoninstaller/log4moz.js, line 494: mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create