package selenium;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
import java.util.Set;
public class ActionSelenium {
public WebDriver driver;
public String windowsHandle;
/**
* 初始化driver
*/
public void InitDriver() {
System.setProperty("webdriver.chrome.driver", "D:\\software\\java-jar+utils\\seleniumForChrome\\chromedriver2.40_win32\\chromedriver.exe");
driver = new ChromeDriver();
//driver.get("https://www.imooc.com/");
//打开慕课网首页
driver.get("https://www.imooc.com/user/newlogin/from_url/");
driver.manage().window().maximize();
}
/**
* 文本框
*/
public void inputBox() {
driver.findElement(By.name("email")).sendKeys("17810299959");
//等待时间
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.name("email")).clear();
String s = driver.findElement(By.name("email")).getAttribute("placeholder");
System.out.println(s);
driver.findElement(By.name("email")).sendKeys("17810299959");
driver.findElement(By.name("password")).sendKeys("123456");
//根据className定位元素,不允许使用符合的类名名称moco-btn moco-btn-red moco-btn-lg btn-full xa-login使用任何一个都行
//driver.findElement(By.className("moco-btn")).click();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 单选框:属性 isSelected,click
*/
public void radioBox() {
driver.get("https://www.imooc.com/user/setprofile");
driver.findElement(By.className("pull-right")).click();
//定位性别,因为name=sex有三个。此方法不好用
//driver.findElement(By.name("sex")).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//用Xpath定位元素,可以定位到。
//driver.findElement(By.xpath("//*[@id=\"profile\"]/div[4]/div/label[1]/input")).click();
//findElement找到一个元素,findElements找到多个元素,可以用集合把它存起来....//input是找到所有的input
List<WebElement> elements = driver.findElements(By.xpath("//*[@id='profile']/div[4]/div/label//input"));
System.out.println(elements.size());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (WebElement radio : elements) {
boolean flag = radio.isSelected();
if (flag == false) {
radio.click();
break;
} else {
System.out.println("选中了");
break;
}
}
try {
//等待两分钟
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 多选框:属性 click ,clear(不能用),isSelected,isEnabled
*/
public void checkBox() {
WebElement check = driver.findElement(By.id("auto-signin"));
System.out.println("是否选中了?" + check.isSelected());
System.out.println("是否有效?" + check.isEnabled());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//check.clear();//clear不能用
check.click();
}
/**
* 按钮:属性 click ,isEnabled
*/
public void button() {
WebElement login = driver.findElement(By.className("moco-btn"));
System.out.println(login.isEnabled());
System.out.println(login.getAttribute("value"));
login.click();
this.sleep(2000);
}
/**
* 表单
*/
public void webForm() {
driver.get("xxxxx");
driver.findElement(By.id("signup-form")).submit();
}
/**
* 上传头像
*/
public void upHeader() {
driver.get("https://www.imooc.com/user/setbindsns");
this.sleep(2000);
String jsString = "document.getElementsByClassName('update-avator')[0].style.bottom='0';";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(jsString);
this.sleep(2000);
driver.findElement(By.className("js-avator-link")).click();
driver.findElement(By.id("upload")).sendKeys("C:\\Users\\xuxueli\\Desktop\\Test.png");
this.sleep(2000);
//点击保存按钮
driver.findElement(By.className("js-avator-save")).click();
}
public void sleep(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 下拉框:
* 属性:index,value,Text;
* 选择:selectByIndex(),
* selectByValue(),
* selectByVisibleText
* 不选择:deselectAll();
* deselectByValue();
* deselectByVisibleText();
*/
public void downSelectBox() {
//个人信息页
driver.get("https://www.imooc.com/user/setprofile");
//编辑按钮
driver.findElement(By.className("js-edit-info")).click();
this.sleep(2000);
//职位设置
//profile父form的Id
WebElement formElement = driver.findElement(By.id("profile"));
WebElement job = formElement.findElement(By.id("job"));
org.openqa.selenium.support.ui.Select downList = new org.openqa.selenium.support.ui.Select(job);
//通过index获取
//downList.selectByIndex(2);
//通过value设置
//downList.selectByValue("1");
//通过Text设置
//downList.selectByVisibleText("学生");
downList.selectByIndex(3);
System.out.println(downList.isMultiple());//false
//downList.deselectByIndex(3);//不用这样用。。。只能再下拉框多选的时候可用
List<WebElement> list = downList.getAllSelectedOptions();//getAllSelectedOptions()适用于下拉多选
for (WebElement option : list) {
System.out.println(option.getText());
}
System.out.println(downList.getFirstSelectedOption().getText());
this.sleep(1000);
}
/**
* 鼠标事件
* 主函数调用时:初始化函数改成打开driver.get("https://www.imooc.com/");
*/
public void mouseAction() {
WebElement login = driver.findElement(By.id("menuContent"));
List<WebElement> item = login.findElements(By.className("item"));
Actions action = new Actions(driver);
//action.click(login).perform();//perform提交操作
//action.doubleClick(login).perform();
//this.sleep(1000);
//action.contextClick(login).perform();
action.moveToElement(item.get(0)).perform();
windowsHandle = driver.getWindowHandle();
driver.findElement(By.linkText("HTML/CSS")).click();
}
/**
* iframe切换
*/
public void iframe() {
driver.get("https://www.imooc.com/wiki/create");
WebElement iframeElement = driver.findElement(By.id("ueditor_0"));
driver.switchTo().frame(iframeElement);
driver.findElement(By.tagName("body")).sendKeys("this is test");
}
/**
* 窗口
*/
public void windowsHandle() {
Set<String> handles = driver.getWindowHandles();
for (String s : handles) {
if (s.equals(windowsHandle)){
continue;
}
System.out.println(s);
driver.switchTo().window(s);
}
driver.findElement(By.linkText("案例")).click();
}
/**
* 等待
*/
public void waitforElement(){
//隐式等待
//driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//显示等待
WebDriverWait wait=new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("test")));
}
public static void main(String[] args) {
ActionSelenium as = new ActionSelenium();
as.InitDriver();
as.inputBox();
as.checkBox();
as.button();
// as.radioBox();
as.upHeader();
as.downSelectBox();
//as.mouseAction();
as.iframe();
//as.windowsHandle();
}
}
java+selenium+慕课网登录实战
猜你喜欢
转载自blog.csdn.net/weixin_40569991/article/details/80843968
今日推荐
周排行