配置:https://www.imooc.com/video/13952
1 建java项目 建包 建类
2 下载import selenium包 并JAVA导入该包。
导入包:右击创建的项目名--选最后一个properties--Java Build Path--右上单击Libraries--右边点击Add External JARs--选择下载解压好的selenium包全部里所有.jar格式全导入-单击OK。
http://npm.taobao.org/mirrors/selenium/ 链接:https://pan.baidu.com/s/1gfzrYs7 密码: rphx
3 需要在Chrome浏览器安装插件ChromeDriver,放置在谷歌浏览器安装目录并加入环境变量path。
http://npm.taobao.org/mirrors/chromedriver/ 链接:https://pan.baidu.com/s/1o8IG7c2 密码: jb3g
4 Chrome 浏览器
package com.TEST;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class openChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys("知乎");
driver.findElement(By.id("su")).click();
driver.findElement(By.name("wd")).sendKeys(" name");
driver.findElement(By.className("s_ipt")).sendKeys(" class_name");
// driver.findElement(By.tagName(""));
driver.findElement(By.linkText("图片")).click();
driver.findElement(By.xpath("//span[@class='col2']/a")).click();
}
}
5 FireFox 浏览器
package com.TEST;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class openFirefox {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.bin", "F:\\APP TEST\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
String testUrl = "http://www.baidu.com";
driver.get(testUrl);
}
}
6 详细Selenium API操作
https://www.imooc.com/video/13960 元素的定位
By.id By.name By.className By.tagName 链接By.linkText
.click()点击 .sendKeys("输入值") .clear()清除 .getText() 获得文本
最好用 By.xpath()
By.xpath('@href')
By.xpath('text()')
By.xpath('//a[contains(text(),"下一页")]/@href')
By.xpath("//span[@class='col2']/a")
By.xpath('//a[contains(text(),'安徽')]/../a[position()>2]')
By.xpath('//ul[@class="l-contact"]/li/span[contains(text(),"公司官网")]/following-sibling::span/a/@href')
driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("hello");
driver.findElement(By.xpath("//span[@class='col2']/a")).click();
driver.findElement(By.xpath("//div[@class='sub-nav']/div[@class='sub-navbar']/a[contains(text(),'供求商机')]")).click();
driver.findElement(By.id("kw")).sendKeys("知乎");;
driver.findElement(By.name("wd")).sendKeys(" name");
driver.findElement(By.className("s_ipt")).sendKeys(" class_name");
driver.findElement(By.linkText("图片")).click();
driver.findElement(By.cssSelector("#kw")).sendKeys("keys");
table定位
// 定位到该表格
WebElement Table = driver.findElement(By.tagName("table"));
// 定位到表格某一列所有元素 定位一组元素用 findElements
// 然后变成list 需要再转化为 WebElement
List<WebElement> rows_0 = Table.findElements(By.tagName("tr"));
// 再循环取出每一行
for(WebElement r:rows_0){
// 取出该行所有列数组
List<WebElement> tds = r.findElements(By.tagName("td"));
for(WebElement t:tds){
System.out.print(t.getText()+"\n");
}
}
操作手册
1 打开网址方式
Driver.get()
Driver.navigate.to()
2 操作浏览器API
Driver.navigate().refresh()
Driver.navigate().forward()
Driver.navigate().back()
Driver.getTitle()
Driver.getCurrenUrl() 获取当前URL
Driver.manage().windows().maximize() 窗口最大化
Driver.quite() 没有完全关闭
Driver.close() 完全关闭
3 滚动条操作 通过JS操作
js.executeScript(document.getElementById("ueditor_0").contentWindow.document.body.innerHTML=\"这个是通过JS写入的在fram里面");
4 操作浏览器cookie
4.1获取cookie
public void getCookie_all(){
Set<Cookie> allCookie = driver.manage().getCookies();
for(Cookie loadedCookie:allCookie){
System.out.print("作用域:"+loadedCookie.getDomain());
名称:loadedCookie.getName()
值:loadedCookie.getValue()
范围:loadedCookie.getArea()
过期时间:loadedCookie.getExpriy()
}
}
4.2删除Cookie
public void decookie(){
message("删除Cookie");
driver.manage().deleteAllCookies();
}
5 输入框操作API
sendKeys()
clear()
getText()
click()
6 选择框处理
6.1单选框
message("定位单选框api进行中");
WebElement radio = driver.findElement(By.name("Sex"));//定位到该单选框
message("检查元素是否选中api进行中");
Checkbox(radio,"单选框");
6.2复选框
message("定位复选框api进行中");
WebElement box = driver.findElement(By.name("Bike"));//定位到该单选框
message("检查元素是否选中api进行中");
6.3下拉框
Checkbox(box,"复选框")
message("下拉框api进行中");
Select list = new Select(driver.findElement(By.name("listname")));
message("下拉框通过index进行选择api进行中");
message("下拉框通过属性进行选择api进行中");
list.selectByValue("下拉框三");
7 特殊窗口处理
7.1 Iframe窗口处理 弹窗处理
7.2页面元素处理
等待加载 根据属性获取元素值 获取对象的css属性
获取对象状态{
页面是否显示isdisplay()
元素是否存在find_element
元素是否选中isSelected()
是否处于灰化状态isEnabled()
窗口处理API
js定位 js单击元素
特殊API
报错时截屏保存
模拟鼠标右键操作(用处不大)
鼠标悬浮(用处不大)
上传文件
日期控制处理
浏览器滚动条操作
这篇博客详细介绍了如何使用JAVA Selenium进行Web自动化测试,包括配置环境、导入Selenium包、安装ChromeDriver并设置环境变量,以及各种元素定位和操作方法,如By.xpath()、click()、sendKeys()等。此外,还涉及了浏览器API、滚动条操作、Cookie管理、选择框处理、弹窗处理等高级功能。

1060

被折叠的 条评论
为什么被折叠?



