package com.selenium.miyang.go;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class login {
WebDriver driver;
public void initDriver(){
System.setProperty("webdriver.chrome.driver", "D:\\wps\\selenium-2.42.2\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.imooc.com/user/newlogin/from_url");
driver.manage().window().maximize();
}
/*
*登陆脚本
*
*/
public void loginScript() throws Exception{
this.initDriver();
String username = "17729597958";
String password = "andong527011764";
String userBy ="name";
String userElement = "email";
String passBy = "name";
String passElement = "password";
String buttonBy = "className";
String buttonElement = "moco-btn";
String headerBy = "id";
String headerElement = "header-avator";
String userInforBy = "className";
String userInforElement = "name";
Thread.sleep(2000);
WebElement user = this.element(this.byStr(userBy, userElement));
user.isDisplayed();
WebElement userpass = this.element(this.byStr(passBy, passElement));
userpass.isDisplayed();
WebElement login_button = this.element(this.byStr(buttonBy, buttonElement));
login_button.isDisplayed();
user.sendKeys(username);
userpass.sendKeys(password);
login_button.click();
Thread.sleep(2000);
WebElement header = this.element(this.byStr(headerBy, headerElement));
Actions action = new Actions(driver);//鼠标悬停
action.moveToElement(header).perform();
String userInfor = this.element(this.byStr(userInforBy, userInforElement)).getText();
System.out.println(userInfor);
if(userInfor.equals("慕勒2472625")){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
}
/*封装By*/
public By byStr(String by,String local ){
if(by.equals("id")){
return By.id(local);
}else if(equals("name")){
return By.name(local);
}else if(by.equals("className")){
return By.className(local);
}else{
return By.xpath(local);
}
}
/*封装element*/
public WebElement element(By by){
WebElement ele=driver.findElement(by);
return ele;
}
public static void main(String[] args) throws Exception{
login action = new login();
action.loginScript();
}
}
|