1.常见的元素处理
2.代码
package com.selenium;
import java.awt.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class ActionSelenium {
public WebDriver driver;
String windowhandle;
public void InitDriver() {
System.setProperty("webdriver.chrome.driver", "C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.imooc.com/user/newlogin");
driver.manage().window().maximize();
}
public void inputBox() {
driver.findElement(By.name("email")).sendKeys("2923732386@qq.com");
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("1836");
driver.findElement(By.name("password")).sendKeys("wojide");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.className("moco-btn-red")).click();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 单选框处理
* radio
* */
public void radioBox() {
driver.get("https://www.imooc.com/user/setprofile");
driver.findElement(By.className("js-edit-info")).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.util.List<WebElement> elements = driver.findElements(By.xpath(".//*[@id='profile']/div[4]/div/lable//input"));
System.out.println(elements.size());
for (WebElement radio : elements) {
boolean flag = radio.isSelected();
if(flag == false) {
radio.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("未选中元素");
break;
}else {
System.out.println("选中元素");
}
}
}
/*
* check 多选框
* */
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
check.click();
}
/*
* 上传文件
* */
public void upFile() {
driver.get("https://www.imooc.com/user/setbindsns");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String strJs = "document.getElementsByClassName('update-avator')[0].style.bottom='0';";
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(strJs);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.className("js-avator-link")).click();
driver.findElement(By.className("js-avator-try")).click();
driver.findElement(By.className("js-avator-save")).click();
System.out.println("头像更换成功");
}
/*
* 下拉框
* 下拉框定位:
* Select list = new Select(locator);
* 选择对应的元素:
* Text、Value、Index
* 不选择对应的元素:
* deselectAll、deselectByValue、deselectByVisibleText、
* 获取选择项的值:
* getAllSelectedOptions()、getFirstSelectedOption().getText;
* */
public void selectBox() {
driver.get("https://www.imooc.com/user/setprofile");
driver.findElement(By.className("pull-right")).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement form = driver.findElement(By.id("profile"));
WebElement element = form.findElement(By.id("job"));
Select select = new Select(element);
select.selectByIndex(2);
}
/*
* 常用的鼠标操作
* 鼠标左击:
* Actions action = new Actions(driver);
* Action.click().perform();
*鼠标右击:
*Actions action = new Actions(driver);
*Action.contextClick().perform();
*鼠标双击:
*Actions action = new Actions(driver);
*Action.doubleClick().perform();
*鼠标悬停:
*Actions action = new Actions(driver);
*Action.moveToElement().perform();
* */
public void mouseBox() {
driver.get("https://www.imooc.com");
driver.manage().window().maximize();
java.util.List<WebElement> group = driver.findElements(By.className("group"));
Actions action = new Actions(driver);
//鼠标悬停
action.moveToElement(group.get(0)).perform();
windowhandle = driver.getWindowHandle();
System.out.println(windowhandle);
driver.findElement(By.linkText("机器学习")).click();
}
/*
* 窗体的分类:iframe、弹窗、对话框
* 对话框、iframe:
* switchTo();
* 弹窗:
* switchTo();
* getwindowHandles()
* */
public void windowHandles() {
Set<String> handle = driver.getWindowHandles();
for (String strWin : handle) {
if(strWin==windowhandle) {
continue;
}
System.out.println(strWin);
driver.switchTo().window(strWin);//转换到当前窗口
}
driver.findElement(By.linkText("深度学习")).click();
}
/*
* 三种形式的等待
* 1.强制等待
* Thread.sleep(2000);
* 2.显示等待
* new webDrverWait(driver,15).until(
*在规定的时间等待,没有执行就报错
* ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator"));
* );
* 3.隐式等待
* Driver.manage().timeouts.implicitlyWait(10,TimeUnit.SECONDS);
* */
public void waitBox() {
driver.findElement(By.id("000999889")).click();
//20秒内找不到元素报错
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ActionSelenium as = new ActionSelenium();
as.InitDriver();
//as.inputBox();
//as.checkBox();
//as.radioBox();
//as.upFile();
//as.selectBox();
//as.mouseBox();
//as.windowHandles();
as.waitBox();
}
}