//执行js
public static void executeJs()
{
//法一
JavascriptExecutor js1 = (JavascriptExecutor)driver;
//1.通过js获取元素
WebElement username = (WebElement)js1.executeScript("document.getElementById(\"j_username\")");
//2.输入用户名
username.clear();
username.sendKeys("username");
//法二
//1.获取元素
WebElement searchButton = driver.findElement(By.id("search"));
//2.js
JavascriptExecutor js = (JavascriptExecutor)driver;
String jsStr = "arguments[0].click();";
//执行js
js.executeScript(jsStr, searchButton);
}
//截屏操作
public static void screenShot()
{
// OutputStream out = null;
//1.new截屏
TakesScreenshot take = (TakesScreenshot)driver;
//2.截出的图片|原始图片位置
File file = take.getScreenshotAs(OutputType.FILE);
//C:\Users\Zhou\AppData\Local\Temp
//3..截屏的目标文件位置
String desPath = "D:/Selenium Auto/Demo/screenshot.jpg";
File destFile = new File(desPath);
//4.删除目标文件位置的图片|路径为文件且不为空则进行删除
if (destFile.isFile()||destFile.exists()) {
destFile.delete();
}
try {
// out = new FileOutputStream(desPath);
// FileUtils.copyFile(file, out);
//5.移动到目标位置
FileUtils.moveFile(file, destFile);
//6.删除默认存储的原始位置图片
file.delete();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//双击操作
public static void doubleClick()
{
//1.
Actions act = new Actions(driver);
//2.找到登录按钮
WebElement submit = driver.findElement(By.id("su"));
// driver.findElement(By.id("kw")).sendKeys("selenium");
//3.双击
// act.doubleClick(submit).build().perform();
act.doubleClick(submit).perform();
}
//拖拽操作
public static void drag()
{
//1.获取元素
WebElement src = driver.findElement(By.id("drag"));
WebElement target = driver.findElement(By.id("drop"));
//2.拖拽
Actions act = new Actions(driver);
act.dragAndDrop(src, target);
}
//下拉列表
public static void select()
{
//1.获取元素
WebElement dropList = driver.findElement(By.id("dropList"));
//2.转为下拉框
Select select = new Select(dropList);
//获取select里的所有option
// List opts = select.getOptions();
// Iterator iter = opts.iterator();
//3.选中第一个
select.selectByIndex(0);
select.selectByValue("选中第一个option");
select.selectByVisibleText("选中第一个option");
}
//单选按钮,复选框操作
public static void radioOrCheckBox()
{
//1.获取元素
WebElement radioOrCheckBox = driver.findElement(By.cssSelector("form input:nth-child(1)"));
//2.点击
radioOrCheckBox.click();
//判断是否转中
radioOrCheckBox.isSelected();
}
Selenium API
package com.selenium;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
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.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class apiTest {
public static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
driver = new FirefoxDriver();
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
String url = "http://baidu.com";
driver.get(url);
//执行js
// executeJs();
//截屏
// screenShot();
//双击
// doubleClick();
//拖拽
// drag();
//下拉列表
// select();
//单选按钮,复选框操作
// radioOrCheckBox();
}
//执行js
public static void executeJs()
{
//法一
JavascriptExecutor js1 = (JavascriptExecutor)driver;
//1.通过js获取元素
WebElement username = (WebElement)js1.executeScript("document.getElementById(\"j_username\")");
//2.输入用户名
username.clear();
username.sendKeys("username");
//法二
//1.获取元素
WebElement searchButton = driver.findElement(By.id("search"));
//2.js
JavascriptExecutor js = (JavascriptExecutor)driver;
String jsStr = "arguments[0].click();";
//执行js
js.executeScript(jsStr, searchButton);
}
//下拉列表
public static void select()
{
//1.获取元素
WebElement dropList = driver.findElement(By.id("dropList"));
//2.转为下拉框
Select select = new Select(dropList);
//获取select里的所有option
// List opts = select.getOptions();
// Iterator iter = opts.iterator();
//3.选中第一个
select.selectByIndex(0);
select.selectByValue("选中第一个option");
select.selectByVisibleText("选中第一个option");
}
//单选按钮,复选框操作
public static void radioOrCheckBox()
{
//1.获取元素
WebElement radioOrCheckBox = driver.findElement(By.cssSelector("form input:nth-child(1)"));
//2.点击
radioOrCheckBox.click();
//判断是否转中
radioOrCheckBox.isSelected();
}
//拖拽操作
public static void drag()
{
//1.获取元素
WebElement src = driver.findElement(By.id("drag"));
WebElement target = driver.findElement(By.id("drop"));
//2.拖拽
Actions act = new Actions(driver);
act.dragAndDrop(src, target);
}
//双击操作
public static void doubleClick()
{
//1.
Actions act = new Actions(driver);
//2.找到登录按钮
WebElement submit = driver.findElement(By.id("su"));
// driver.findElement(By.id("kw")).sendKeys("selenium");
//3.双击
// act.doubleClick(submit).build().perform();
act.doubleClick(submit).perform();
}
//截屏操作
public static void screenShot()
{
// OutputStream out = null;
//1.new截屏
TakesScreenshot take = (TakesScreenshot)driver;
//2.截出的图片|原始图片位置
File file = take.getScreenshotAs(OutputType.FILE);
//C:\Users\Zhou\AppData\Local\Temp
//3..截屏的目标文件位置
String desPath = "D:/Selenium Auto/Demo/screenshot.jpg";
File destFile = new File(desPath);
//4.删除目标文件位置的图片|路径为文件且不为空则进行删除
if (destFile.isFile()||destFile.exists()) {
destFile.delete();
}
try {
// out = new FileOutputStream(desPath);
// FileUtils.copyFile(file, out);
//5.移动到目标位置
FileUtils.moveFile(file, destFile);
//6.删除默认存储的原始位置图片
file.delete();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void api(){
// 创建了一个 Firefox driver 的实例
// 注意,其余的代码依赖于接口而非实例
driver = new FirefoxDriver();
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
// 使用它访问 Google
driver.get("http://www.google.com");
// 同样的事情也可以通过以下代码完成
// driver.navigate().to("http://www.google.com");
// 找到搜索输入框
WebElement element = driver.findElement(By.name("q"));
// 输入要查找的词
element.sendKeys("Cheese!");
// 提交表单
element.submit();
// 检查页面标题
System.out.println("Page title is: " + driver.getTitle());
// Google 搜索结果由 JavaScript 动态渲染
// 等待页面加载完毕,超时时间设为10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
//应该能看到: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//关闭浏览器
driver.quit();
}
}