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.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class ActionSelenium {
public WebDriver driver;
public String windowsHandle;
public void InitDriver() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
//老版本的登陆页面
//driver.get("http://www.sacon.com/user/newlogin/from/url/");
driver.get("http://www.sacon.com/");//默认首页
driver.manage().window().maximize();
}
public void inputBox() {
// driver.findElement(By.name("email")).sendKeys("email");
//
// 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("email");
driver.findElement(By.name("password")).sendKeys("password");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 单选框
*
*/
public void radioBox() {
driver.get("https://www.sacon.com/user/setprofile");
driver.findElement(By.className("pull-right")).click();
//driver.findElement(By.xpath("//*[@id=\"profile\"]/div[4]/div/label[1]/input")).click();
List elements = driver.findElements(By.xpath("//*[@id=\"profile\"]/div[4]/div/label//input"));
System.out.println(elements.size());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(WebElement radio:elements) {
boolean flag = radio.isSelected();
if(flag==false) {
radio.click();
break;
}else {
System.out.println("选中了...");
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void checkBox() {
WebElement check = driver.findElement(By.id("auto-signin"));
System.out.println("是否选中了checkbox?"+check.isSelected());
System.out.println("checkbox是否有效?"+check.isEnabled());
//check.clear();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
check.click();
}
/**
*
* 按钮
*/
public void button() {
WebElement login = driver.findElement(By.className("moco-btn-red"));
System.out.println(login.isEnabled());
System.out.println(login.getAttribute("value"));
driver.findElement(By.className("moco-btn-red")).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void webForm() {
// driver.get("xxx");
driver.findElement(By.id("signin-form")).submit();;
}
/**
*
* 上传文件
* */
public void upHeader() {
driver.get("https://www.sacon.com/user/setbindsns");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String jsString = "document.getElementsByClassName('update-avator')[0].style.buttom=0";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(jsString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.className("update-avator")).click();
driver.findElement(By.id("upload")).sendKeys("C:\\Users\\Administrator\\Desktop\\test.jpg");
driver.findElement(By.className("js-avator-save")).click();
}
/**
*
* 下拉框操作
* */
public void downSelectBox() {
driver.get("https://www.sacon.com/user/setprofile");
driver.findElement(By.className("pull-right")).click();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement formElement = driver.findElement(By.id("profile"));
WebElement job = formElement.findElement(By.id("job"));
Select downList = new Select(job);
// downList.selectByIndex(3);
// downList.selectByValue("6");
// downList.selectByVisibleText("学生");
downList.selectByIndex(1);
List List = downList.getAllSelectedOptions();
for(WebElement option:List) {
System.out.println(option.getText());
}
System.out.println(downList.isMultiple());
// downList.deselectByIndex(3);
System.out.println(downList.getAllSelectedOptions().get(0));
System.out.println(downList.getFirstSelectedOption().getText());
}
/**
*
*
* 鼠标事件
* */
public void mouseAction() {
//WebElement login = driver.findElement(By.id("js-signin-btn"));
WebElement login = driver.findElement(By.className("menuContent"));
List item = login.findElements(By.className("item"));
Actions actions = new Actions(driver);
//actions.click(login).perform();
//actions.doubleClick(login).perform();
//this.sleep(2000);
//actions.contextClick(login).perform();
actions.moveToElement(item.get(0)).perform();
windowsHandle = driver.getWindowHandle();
driver.findElement(By.linkText("以太坊")).click();
}
/**
* iframe 切换
* */
public void iframe() {
driver.get("http://www.sacon.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 windowHandle() {
Set 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(20, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("test")));
}
private void sleep(int i) {
// TODO Auto-generated method stub
try {
Thread.sleep(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ActionSelenium as = new ActionSelenium();
as.InitDriver();
as.checkBox();
as.waitforElement();
as.inputBox();
as.button();
as.upHeader();
as.downSelectBox();
as.mouseAction();
as.iframe();
as.windowHandle();
}
}