--加载驱动
System.out.println("开始web自动化!!");
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
// System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
--火狐浏览器
System.setProperty("webdriver.firefox.bin","D:\\dailyTool\\Mozilla Firefox\\firefox.exe");
--使用浏览器默认配置
// ProfilesIni pi = new ProfilesIni();
// FirefoxProfile profile = pi.getProfile("default");
FirefoxProfile profile = new FirefoxProfile();
driver = new FirefoxDriver(profile);
driver.get("http://www.jd.com/");
driver.findElement(By.linkText("你好,请登录")).click();
// driver.findElement(By.id("loginname")).click();
给输入框设置数据XXX
driver.findElement(By.id("loginname")).sendKeys("XXX");
// driver.findElement(By.id("nloginpwd")).click();
driver.findElement(By.id("nloginpwd")).sendKeys("");
driver.findElement(By.id("loginsubmit")).click();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("这里是之前的windows:"+driver.getWindowHandle());
driver.findElement(By.linkText("我的订单")).click();
--注意这里,这里是跳转新的链接,需要判断需要操作的界面,然后switchTo
Set
handles = driver.getWindowHandles();
WebDriver driver4 = null;
int count = 0;
for (String s : handles) {
if(count == 1){
System.out.println("这里是新跳转的页面集合:"+s);
driver4 = driver.switchTo().window(s);
break;
}
count ++;
}
--这里就是著名Xpath解析,万能无敌之好用。找不到大部分可能是跳转的页面不正确
driver4.findElement(By.xpath("//span[@class='signup-btn']")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
--退出
driver.quit();