Selenium工作原理
SeleniumAPI
定位元素
Selenium操作对象
**send_keys 在对象上模拟按键输入 **
clear 清除对象输入的文本内容 **
click 点击对象(无限制)
submit 提交(用于form表单) **
getText() 用于获取元素的文本信息
getAttribute() 用于获取属性的值
quit 关闭游览器(quit关闭会将token cookie等删除 推荐)
close 关闭游览器(不推荐)
隐式等待 selenium.webdriver.remote.webdriver.implicitly_wait(time_to_wait)
使用案例:
public static void main(String[] args) throws InterruptedException {
//创建一个游览器驱动
WebDriver webDriver = new ChromeDriver();
//打开百度网页
webDriver.get("https://www.baidu.com");
//找到搜索框
WebElement cearch_input = webDriver.findElement(By.cssSelector("#kw"));
//输入软件测试
cearch_input.sendKeys("软件测试");
//清空搜索框中内容
cearch_input.clear();
//输入前端
cearch_input.sendKeys("前端");
//找到百度一下,点击
WebElement baidu_button = webDriver.findElement(By.cssSelector("#su"));
baidu_button.click();
//隐式等待
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//显示等待
//WebDriverWait wait = new WebDriverWait(webDriver, 3);
//wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#su")));
//查看是否有前端相关内容
List<WebElement> search_result = webDriver.findElements(By.xpath("//font[@color=\"#CC0000\"]"));
for (int i = 0; i < search_result.size(); i++) {
if(search_result.get(i).getText().equals("前端")) {
System.out.println("成功");
}else{
System.out.println("失败");
}
}
//关闭游览器 quit关闭会将token cookie等删除 推荐
webDriver.quit();
}
打印信息
获取title getTitle()
获取url getCurrentUrl()
游览器操作
游览器最大化 webDriver.manage.windows.maximize()
设置游览器大小 webDriver.manage().window().setSize(new Dimension(x, y))
游览器前进后退 webDriver.navigate().back() / forward()
操作游览器滚动 ((JavascriptExecutor)webDriver).executeScrpt(“document.documentElement.scrollTop=x”)
键盘事件
键盘按键用法和键盘组合用法:
鼠标事件
移动和右键:
public static void main(String[] args) {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://baidu.com/");
//先创建一个Actions
Actions actions = new Actions(webDriver);
WebElement element = webDriver.findElement(By.xpath("//a[@target=\"_blank\"]"));
//鼠标移动到这个按钮上且右键
actions.moveToElement(element).contextClick().perform();
}
特殊操作
切换窗口
//切换窗口句柄
public static void main10(String[] args) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://baidu.com/");
webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
sleep(3000);
//获取当前窗口句柄
String cur_handle = webDriver.getWindowHandle();
System.out.println(cur_handle);
//获取游览器全部窗口的句柄
Set<String> all_handle = webDriver.getWindowHandles();
for (String str : all_handle) {
if (!str.equals(cur_handle)) {
//将句柄切换到当前窗口句柄
webDriver.switchTo().window(str);
}
}
webDriver.findElement(By.cssSelector("#ww")).sendKeys("测试");
webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
sleep(3000);
List<WebElement> webElements = webDriver.findElements(By.cssSelector("#\\32 > div > h3 > a > em"));
System.out.println(webElements.toString());
for (int i = 0; i < webElements.size(); i++) {
if(webElements.get(i).getText().equals("测试")) {
System.out.println("测试成功");
} else {
System.out.println("测试失败");
}
}
截图
引入依赖:
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.0</version>
</dependency>
public static void main(String[] args) throws IOException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://baidu.com/");
//截图 把截下来的图放到File临时变量中
File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
//把File保存在磁盘中
FileUtils.copyFile(file, new File("D:/测试.png"));
}
选择一组元素
public static void main(String[] args) {
WebDriver webDriver = new ChromeDriver();
webDriver.get("");
List<WebElement> webElements = webDriver.findElements(By.cssSelector(""));
for(int i = 0; i < webElements.size(); i++) {
if (webElements.get(i).getAttribute("").equals("")) {
webElements.get(i).click();
}
}
}
定位iframe下标签
private static void Page02() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/Test01/Page/test02.html?_ijt=hsrkct8hrvk081crvik4rmae0k&_ij_reload=RELOAD_ON_SAVE");
webDriver.switchTo().frame("f1");
sleep(3000);
webDriver.findElement(By.cssSelector("body > div > div > a")).click();
}
定位下拉框选项
//定位下拉框选项
public static void main3(String[] args) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/measurement/page/a.html?_ijt=fpqtishuhhavpgqi7lpf6mti4p&_ij_reload=RELOAD_ON_SAVE");
WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
//创建一个Select对象
Select select = new Select(webElement);
//第一种方式 通过序号选中选项, 下标从0开始
select.selectByIndex(2);
sleep(3000);
select.selectByValue("12.51");
}
定位弹窗
//定位弹窗
public static void main4(String[] args) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("file:///D:/code/java-ee/code/measurement/src/main/java/page/b.html");
webDriver.findElement(By.cssSelector("body > button")).click();
//弹框里输入啊哈哈哈哈哈哈
webDriver.switchTo().alert().sendKeys("啊哈哈哈哈哈哈");
sleep(3000);
//弹框确定
webDriver.switchTo().alert().accept();
/*弹框取消
webDriver.switchTo().alert().dismiss();*/
String txt = webDriver.findElement(By.cssSelector("body > div")).getText();
if(txt.equals("啊哈哈哈哈哈哈")) {
System.out.println("测试成功");
}else {
System.out.println("测试失败");
}
}
上传文件
//上传文件
public static void main(String[] args) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("file:///D:/code/java-ee/code/measurement/src/main/java/page/c.html");
sleep(3000);
//上传文件
webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("D:\\brower\\网络原理2.png");
}