selenium官方下载地址:http://selenium-release.storage.googleapis.com/index.html
百度网盘下载地址:
链接:https://pan.baidu.com/s/1UAgNiSISyusW9hqgrlTYPQ
提取码:bxps
复制这段内容后打开百度网盘手机App,操作更方便哦
配置
将三个小牛奶瓶添加以来
然后右键添加Junit Test Case
配置浏览器的目录环境变量中D:\Program Files我配置的
将驱动和浏览器的.exe放在一起
执行System.setProperty("webdriver.gecko.driver","D:\Program Files\geckodriver.exe");
就行了
//指定元素
WebElement wb = driver.findElement(By.id("submit"));
// 拖动到可见的元素去
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", wb);
基本操作
//拖动像素
滚动页面的方法:
window.scrollBy()
window.scrollBy(0,500) 向下滚动500个像素
window.scrollBy(0,-500) 向上滚动500个像素
window.scrollBy(500,0) 向右滚动500个像素
window.scrollBy(-500,0) 向左滚动500个像素
package yanhcenglong;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class jsyu {
@Test
public void test() throws Throwable {
System.setProperty("webdriver.gecko.driver", "D:\\Program Files\\geckodriver.exe");
WebDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.get("https://www.baidu.com");
firefoxDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// firefoxDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
String js="document.getElementById('kw').value='北才'";
if(firefoxDriver instanceof JavascriptExecutor){
JavascriptExecutor js2=(JavascriptExecutor)firefoxDriver;
js2.executeScript(js);
firefoxDriver.findElement(By.id("su")).click();
String resultpath="//*[@id='container']/div[2]/div/div[2]/span";
Thread.sleep(3000);
String text = firefoxDriver.findElement(By.xpath(resultpath)).getText();
System.out.println(text);
String[] split = text.split("[约个]");
for(String s:split){
System.out.println(s);
}
}
}
}
我写的工具类
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import junit.framework.TestCase;
public class ToolDriver {
//定义一个path,用于找不到元素时取得截图路径 例如:E:\\screenshot 要用双反斜线
public static String imgpath = "E:\\screenshot";
static String day = (new SimpleDateFormat("yyyy-mm-dd")).format(new Date());
static String time = (new SimpleDateFormat("hh-MM-ss")).format(new Date());
public static String logpath = "E:\\log\\"+day+"\\"+time+".txt";
public static WebDriver ToolDriver;
/**
*
* 获取Chrome的WebDriver
* @return
*/
public static WebDriver getChromeDriver(){
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
ToolDriver=driver;
return driver;
}
/**
* Firefox
* 获取火狐浏览器
* @return WebDriver
*/
public static WebDriver getFirefoxDriver(){
System.setProperty("webdriver.gecko.driver","D:\\Program Files\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
return driver;
}
public static WebElement xpathElement(String Element) {
return null;
}
/**
* 点击元素
*/
public static void clickElement(String Element) {
try {
ToolDriver.findElement(By.xpath(Element)).click();
}catch (Exception e) {
System.out.println("点击失败");
}
}
/**
* 输入
*/
public static void sendkeyElement(String Element,String sendkey) {
ToolDriver.findElement(By.xpath(Element)).sendKeys(sendkey);
}
/**
* 点击并提示错误
*/
public static void clickElement(String Element,String info) {
try {
ToolDriver.findElement(By.xpath(Element)).click();
}catch (Exception e) {
System.out.println("点击"+info+"--失败");
}
}
/**
* 点击并提示错误
*/
public static void clickElementSleep(String Element,String info,int number) {
//循环的测试
for(int i=0;i getList(String element) {
List findElements = ToolDriver.findElements(By.xpath(element));
return findElements;
}
/**
* 获取List的元素并增加错误提示
*/
public static List getList(String element,String into) {
List findElements=null;
try {
findElements = ToolDriver.findElements(By.xpath(element));
}catch(Exception e){
System.out.println("get"+into+"error");
return null;
}
return findElements;
}
/**
* 输入元素并增加错误提示
*/
public static void sendkeyElement(String Element,String sendkey,String info) {
try {
ToolDriver.findElement(By.xpath(Element)).sendKeys(sendkey);
}catch (Exception e) {
System.out.println("输入"+info+"--失败");
}
}
/**
* 杀掉firefox的进程
*/
public static void killThread(){
Runtime runTime = Runtime.getRuntime();
try {
runTime.exec("taskkill /F /IM firefox.exe");
// taskkill在目录c:windows/system32中,需要配置环境变量
// 有同学在cmd执行tskill chrome不是内部命令,可以改为
// taskkill /F /IM chrome.exe
// runTime.exec("tskill chromedriver");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查找id元素,并增加等待时间。
*/
public static WebElement waitandfindElementByid(WebDriver driver,String id){
//循环的测试
int number = 3;
WebElement wb = null;
for(int i=0;i set){
List list = new ArrayList(set);
// 定义一个新开页面的handle
String newtabhandle = null;
// 当打开新页面时,得到的句柄集合
Set newwinHandles = driver.getWindowHandles();
Iterator newit = newwinHandles.iterator();
while(newit.hasNext()){
String temp = newit.next().toString();
// 把打开第三个页面的句柄依次取出,和之前的句柄进行判断,得到新开页面的句柄
if(!list.contains(temp)){
newtabhandle = temp;
}
}
return newtabhandle;
}
/**根据传入路径,只需传入目录即可,保存截图
* @param i
*/
public static void getScreenShot(WebDriver driver,String path){
Date date= new Date();
SimpleDateFormat daysdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timesdf = new SimpleDateFormat("yyyymmdd-HHmmss");
String daydir = daysdf.format(date);
String timedir = timesdf.format(date);
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(file, new File(path+"\\"+daydir+"\\"+timedir+".png"));
System.out.println("ok");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 报错的log输出文件查看
* @param log
*/
public static void writeLog(String log){
try {
FileWriter fw = new FileWriter(logpath);
fw.write(log,0,log.length());
fw.flush();
fw.close();
} catch (IOException ef) {
ef.printStackTrace();
}
}
}
补充可以用:
第二版
package send.data.chrome.tool;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ToolSet {
final static int TEST_1=1;
final static int FORMAL_2=2;
public WebDriver ToolDriver;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
private String element = "";
//创建 SingleObject 的一个对象
private static ToolSet instance = new ToolSet();
//让构造函数为 private,这样该类就不会被实例化
private ToolSet(){}
//获取唯一可用的对象
public static ToolSet getInstance(){
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
System.setProperty("webdriver.ie.driver","src/main/resources/IEDriverServer.exe");
System.setProperty("webdriver.gecko.driver","src/main/resources/geckodriver.exe");
System.setProperty("webdriver.edge.driver","src/main/resources/msedgedriver.exe");
return instance;
}
String absolutePath2=null;
/**根据传入路径,只需传入目录即可,保存截图
* @param i
*/
public boolean getScreenShot(String elt,String prName){
WebElement findElement = ToolDriver.findElement(By.xpath(elt));
File file = findElement.getScreenshotAs(OutputType.FILE);
try {
File file2 = new File(prName);
FileUtils.copyFile(file,file2);
absolutePath2=file2.getAbsolutePath();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public String getImgPath() {
return absolutePath2;
}
/**关闭浏览器
*
*/
public void close() {
ToolDriver.close();
}
/**
* 进入:iframe
*/
public ToolSet switchToFrame(String Element,String info) {
try {
ToolDriver.switchTo().frame(Element);
}catch (Exception e) {
System.out.println(info+"--失败");
}
return instance;
}
/**
* 退出iframe
*/
public ToolSet defaultContent() {
try {
ToolDriver.switchTo().defaultContent();
}catch (Exception e) {
System.out.println("--失败");
}
return instance;
}
/**
*
* 获取Chrome的WebDriver
* @return
*/
public WebDriver getChromeDriver(){
ToolDriver = new ChromeDriver();
return ToolDriver;
}
public WebDriver getEdgeDriver(){
ToolDriver = new EdgeDriver();
return ToolDriver;
}
public WebDriver getFirefoxDriver(){
ToolDriver = new FirefoxDriver();
return ToolDriver;
}
public void getUrl(String url) {
ToolDriver.manage().window().maximize();
ToolDriver.get(url);
}
/**
*
* 获取ie的WebDriver
* @return
*/
public WebDriver getIeDriver(){
ToolDriver = new InternetExplorerDriver();
return ToolDriver;
}
/**
* 点击并提示错误
*/
public ToolSet clickElement(String Element,String info) {
try {
ToolDriver.findElement(By.xpath(Element)).click();
}catch (Exception e) {
System.out.println(info+"--失败");
}
return instance;
}
/**
* 点击并提示错误
*/
public ToolSet clickElement(String info) {
try {
ToolDriver.findElement(By.xpath(Element)).click();
}catch (Exception e) {
System.out.println(info+"--失败");
}
return instance;
}
/**
* 获取List的元素并增加错误提示
*/
public List getList(String element,String into) {
List findElements=null;
try {
findElements = ToolDriver.findElements(By.xpath(element));
}catch(Exception e){
System.out.println("get"+into+"error");
return null;
}
return findElements;
}
/**
* 杀掉firefox的进程
*/
public void killThread(){
Runtime runTime = Runtime.getRuntime();
try {
runTime.exec("taskkill /F /IM firefox.exe");
// taskkill在目录c:windows/system32中,需要配置环境变量
// 有同学在cmd执行tskill chrome不是内部命令,可以改为
// taskkill /F /IM chrome.exe
// runTime.exec("tskill chromedriver");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查找xpath元素,并增加等待时间。
*/
public static WebElement waitandfindElementByxpath(WebDriver driver,String xpath){
//循环的测试
int number = 3;
WebElement wb = null;
for(int i=0;i> 出现
new WebDriverWait(ToolDriver,10).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(Element)));
System.out.println("显示等待");
return instance;
}
/**
* 显示等待
*/
@SuppressWarnings("deprecation")
public ToolSet setDisplay() {
//显示等待时间10s 等 全部产品>> 出现
new WebDriverWait(ToolDriver,10).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(Element)));
System.out.println("显示等待");
return instance;
}
/**
* 点击按钮
*/
public ToolSet clearElement(String Element,String info) {
try {
ToolDriver.findElement(By.xpath(Element)).clear();
}catch(Exception e) {
System.out.println(info+"--失败");
}
return instance;
}
/**
* 点击按钮
*/
public ToolSet clearElement(String info) {
try {
ToolDriver.findElement(By.xpath(Element)).clear();
}catch(Exception e) {
System.out.println(info+"--失败");
}
return instance;
}
/**
* 点击按钮
*/
public ToolSet clearElement() {
try {
ToolDriver.findElement(By.xpath(Element)).clear();
}catch(Exception e) {
System.out.println("点击--失败");
}
return instance;
}
/**
* 输入元素并增加错误提示
*/
public ToolSet sendkeyElement(String Element,String sendkey,String info) {
try {
ToolDriver.findElement(By.xpath(Element)).sendKeys(sendkey);
}catch (Exception e) {
System.out.println(info+"--失败");
}
return instance;
}
/**
* 获取输入的元素
*/
public String getAttribute(String Element,String info) {
String attribute=null;
try {
attribute = ToolDriver.findElement(By.xpath(Element)).getAttribute("value");
}catch (Exception e) {
System.out.println(info+"--失败");
}
return attribute;
}
/**
* 输入元素并增加错误提示
*/
public ToolSet sendkeyElement(String sendkey,String info) {
try {
ToolDriver.findElement(By.xpath(Element)).sendKeys(sendkey);
}catch (Exception e) {
System.out.println(info+"--失败");
}
return instance;
}
/**
* 输入元素并增加错误提示
*/
public ToolSet sendkeyElement(String sendkey) {
try {
ToolDriver.findElement(By.xpath(Element)).sendKeys(sendkey);
}catch (Exception e) {
System.out.println("输入"+sendkey+"失败");
}
return instance;
}
public ToolSet sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return instance;
}
/**
* 输入元素并增加错误提示
*/
public String getText(String Element,String info) {
String text;
try {
text = ToolDriver.findElement(By.xpath(Element)).getText();
}catch (Exception e) {
System.out.println(info+"--失败");
text="未获取到文本";
}
return text;
}
/**
* 输入元素并增加错误提示
*/
public String getText(String Element) {
String text;
try {
text = ToolDriver.findElement(By.xpath(Element)).getText();
}catch (Exception e) {
System.out.println("获取--失败"+Element);
text="未获取到文本";
}
return text;
}
/**
* 输入元素并增加错误提示
*/
public String getText() {
String text;
try {
text = ToolDriver.findElement(By.xpath(Element)).getText();
}catch (Exception e) {
System.out.println("--失败");
text="未获取到文本";
}
return text;
}
/**
* 循环点击
*/
public void clickElementSleep(String Element,String info,int number) {
//循环的测试
for(int i=0;i