常用的鼠标操作
- 鼠标左击
- 鼠标右击
- 鼠标双击
- 鼠标悬停
鼠标左击
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();
package com.test.selenium;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class ActionSelenium {
public WebDriver driver;
public void InitDriver(){
System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
//driver.navigate().to("https://www.imooc.com/user/newlogin");
driver.navigate().to("https://www.imooc.com/");
driver.manage().window().maximize();
}
public void sleep(int x){
try{
Thread.sleep(x);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public void inputBox(){
//driver.findElement(By.name("email")).sendKeys("895236981@qq.com");
this.sleep(2000);
//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("895236987@qq.com");
driver.findElement(By.name("password")).sendKeys("sss");
//driver.findElement(By.className("moco-btn-red")).click();
this.sleep(2000);
}
/**
* 单选框
*
* */
public void radioBox(){
driver.get("https://www.imooc.com/user/setprofile");
driver.findElement(By.className("pull-right")).click();
//driver.findElement(By.xpath(".//*[@id='profile']/div[4]/div/label[1]")).click();
List<WebElement> elements = driver.findElements(By.xpath(".//*[@id='profile']/div[4]/div/label//input"));
System.out.println(elements.size());
this.sleep(2000);
for(WebElement radio:elements){
boolean flag = radio.isSelected();
if(flag==false){
radio.click();
break;
}else{
System.out.println("选中了");
}
}
}
/**
* 复选框
* */
public void checkBox(){
WebElement check = driver.findElement(By.id("auto-signin"));
System.out.println("是否选中了呢?" + check.isSelected());
System.out.println("是否有效?" + check.isEnabled());
//check.clear(); //不需要使用
this.sleep(2000);
check.click();
}
/**
* 按钮
* */
public void button(){
WebElement login = driver.findElement(By.className("moco-btn"));
System.out.println(login.isEnabled());
System.out.println(login.getAttribute("value"));
login.click();
}
/**
* 表单
* */
public void webForm(){
driver.get("XXXXX");
driver.findElement(By.id("signup-form")).submit();
}
/**
* 上传文件
* */
public void upHeader(){
this.sleep(2000);
driver.get("https://www.imooc.com/user/setbindsns");
driver.findElement(By.className("js-avator-link")).click();
driver.findElement(By.id("upload")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\logd.png");
}
/**
* 下拉框操作
* */
public void downSelectBox(){
this.sleep(2000);
driver.get("https://www.imooc.com/user/setprofile");
driver.findElement(By.className("pull-right")).click();
this.sleep(1000);
WebElement formElement = driver.findElement(By.id("profile"));
WebElement job = formElement.findElement(By.id("job"));
Select downList = new Select(job);
//downList.selectByIndex(2);
//downList.selectByValue("1");
//downList.selectByVisibleText("学生");
downList.selectByIndex(2);
//System.out.println(downList.isMultiple());
//downList.deselectByIndex(3);
List<WebElement> List = downList.getAllSelectedOptions();
for(WebElement option:List){
System.out.println(option.getText());
}
System.out.println(downList.getFirstSelectedOption().getText());
}
/**
* 鼠标事件
* */
public void moseAction(){
WebElement login = driver.findElement(By.className("menuContent"));
List <WebElement> 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(1)).perform();
driver.findElement(By.linkText("HTML/CSS")).click();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ActionSelenium as = new ActionSelenium();
as.InitDriver();
//as.inputBox();
//as.checkBox();
//as.button();
//as.upHeader();
//as.downSelectBox();
as.moseAction();
}
}