selenium 是用来做web自动化测试的框架,支持各种浏览器,各种,支持各种语言
原理:
绝对路径: /html/head/title
相对路径以双斜杠开头,常见的相对路径定位有以下几种:
<1>相对路径+索引: 索引是从1开始的
<2>相对路径+属性值:
<3>相对路径+通配符
<4>相对路径+文本匹配
• id选择器: #id
• 类选择器: .class
• 标签选择: 标签名
• 后代选择器: 父级选择器 子级选择器
• click 点击对象
• send_keys 在对象上模拟按键输入
• clear 清除对象输入的文本内容
• submit 提交
• getAttribute 获取标签中value属性所对应的值
• text 由于获取元素的文本信息
public class Demo1 {
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
//允许所有请求
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver =new ChromeDriver(options);
//获取网址
webDriver.get("https://www.sogou.com");
//获取value标签元素文本信息
String str=webDriver.findElement(By.xpath("//input[@value=\"搜狗搜索\"]")).getAttribute("value");
System.out.println(str);
//输入搜索内容
webDriver.findElement(By.cssSelector("#query")).sendKeys("软件测试");
Thread.sleep(3000);
webDriver.findElement(By.xpath("//input[@value=\"搜狗搜索\"]")).click();
Thread.sleep(3000);
//找到并打印所有a标签下em标签中的内容
List elements=webDriver.findElements(By.cssSelector("a em"));
for (int i = 0; i < elements.size(); i++) {
System.out.println(elements.get(i).getText());
}
Thread.sleep(3000);
webDriver.close();
}
}
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.sogou.com/");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys("软件测试");
Thread.sleep(3000);
//由于此处的搜狗搜索在form标签中,因此能够顺利提交
//webDriver.findElement(By.cssSelector("#stb")).click();
webDriver.findElement(By.cssSelector("#stb")).submit();
Thread.sleep(3000);
//此时代码是会报错的,因为a标签并不在form标签内
//webDriver.findElement(By.cssSelector("#weixinch")).submit();
webDriver.close();
}
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.baidu.com/");
webDriver.findElement(By.cssSelector("#s-top-loginbtn")).click();
//隐式等待
//webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
//显示等待,若加载出直接执行下面代码,若在指定时间内没有加载出来,就抛异常
new WebDriverWait(webDriver,10).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#s-top-loginbtn")));
//强制等待3s
Thread.sleep(3000);
webDriver.findElement(By.xpath("//*[@id=\"TANGRAM__PSP_11__userName\"]")).sendKeys("1111");
webDriver.close();
}
隐式等待等待的是整个页面的元素,而显示等待等待的是一定的条件.
public static void main(String[] args) {
ChromeOptions options =new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.sogou.com/");
String title=webDriver.getTitle();
String url=webDriver.getCurrentUrl();
System.out.println("当前标题:"+title+"当前url:"+url);
webDriver.close();
}
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.sogou.com");
webDriver.findElement(By.cssSelector("#query")).sendKeys("软件测试");
webDriver.findElement(By.cssSelector("#stb")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
//后退一步
webDriver.navigate().back();
Thread.sleep(3000);
//前进一步
webDriver.navigate().forward();
Thread.sleep(3000);
//使屏幕最大化
webDriver.manage().window().maximize();
Thread.sleep(3000);
//全屏
webDriver.manage().window().fullscreen();
Thread.sleep(3000);
//自定义窗口大小
webDriver.manage().window().setSize(new Dimension(600,1000));
Thread.sleep(3000);
//滑动滚动条
((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=19999");
}
通过sendKeys()调用按键:
• sendkeys(Keys.TAB) #TAB
• sendKeys(Keys.ENTER) #回车
• sendKeys(Keys.SPACE) #空格键
• sendKeys(Keys.ESCAPE) #回退键(Esc)
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.sogou.com/");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys("软件测试");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys(Keys.SPACE);
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys("软件开发");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys(Keys.ENTER);
}
键盘组合键用法
sendKeys(Keys.CONTROL,"a") #全选 (Ctrl+a)
sendKeys(Keys.CONTROL,"c") #复制 (Ctrl+c)
sendKeys(Keys.CONTROL,"x") #剪切 (Ctrl+x)
sendKeys(Keys.CONTROL,"v") #粘贴 (Ctrl+v)
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.sogou.com/");
webDriver.findElement(By.cssSelector("#query")).sendKeys("软件测试");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys(Keys.CONTROL,"a");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys(Keys.CONTROL,"x");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys(Keys.CONTROL,"v");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#query")).sendKeys(Keys.ENTER);
}
Actions类:
• contextClick() 右击
• doubleClick() 双击
• dragAndDrop() 拖动
• moveToElement() 移动
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.sogou.com/");
webDriver.findElement(By.cssSelector("#query")).sendKeys("软件测试");
webDriver.findElement(By.cssSelector("#query")).sendKeys(Keys.ENTER);
Actions actions=new Actions(webDriver);
Thread.sleep(3000);
//需要现将鼠标移动到要操作的元素,然后右击,要perform()才会有效果
actions.moveToElement( webDriver.findElement(By.cssSelector("#sogou_weixin"))).contextClick().perform();
}
为了方便测试的演示,测试的页面都是自制的。
页面:
Document
checkbox
测试:
public static void main(String[] args) {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("http://localhost:63342/SeleniumTest/page/test1.html?_ijt=a28mk13t2kbijoe7d2clon53lj&_ij_reload=RELOAD_ON_SAVE");
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.DAYS);
List webElements=webDriver.findElements(By.xpath("//input[@type=\"checkbox\"]"));
for (int i = 0; i < webElements.size(); i++) {
System.out.println(webElements.get(i).getAttribute("type"));
}
}
多框架定位
如果有内嵌网页框架,需要先转到框架才能操作框架内元素。
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
//允许所有请求
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver =new ChromeDriver(options);
webDriver.get("https://mail.163.com/");
//需要先定位到框架,再对框架内元素进行操作
webDriver.switchTo().frame(webDriver.findElement(By.xpath("//iframe")));
Thread.sleep(3000);
webDriver.findElement(By.xpath("//input[@name=\"email\"]")).sendKeys("12345");
}
窗口的切换
在浏览器中每个窗口都有一个句柄来标识
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("Https://www.baidu.com");
//获取当前句柄
String handle= webDriver.getWindowHandle();
System.out.println(handle);
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
Set hanles=webDriver.getWindowHandles();
for (String h:hanles) {
handle=h;
}
webDriver.switchTo().window(handle);
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
Thread.sleep(3000);
webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
}
下拉框
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("http://localhost:63342/SeleniumTest/page/test3.html?_ijt=dcl94qtill9arl6odicib469be&_ij_reload=RELOAD_ON_SAVE");
WebElement webElement=webDriver.findElement(By.cssSelector("#ShippingMethod"));
Select select=new Select(webElement);
Thread.sleep(3000);
//通过标签 value选择
select.selectByValue("9.03");
Thread.sleep(3000);
//通过下标选择,下标从零开始
select.selectByIndex(2);
}
Title
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("http://localhost:63342/SeleniumTest/page/test4.html?_ijt=e7mju27ab5d214o41bhvcqjf4r&_ij_reload=RELOAD_ON_SAVE");
webDriver.findElement(By.xpath("//*[text()=\"这是一个弹窗\"]")).click();
Thread.sleep(3000);
//alert弹窗取消
webDriver.switchTo().alert().dismiss();
Thread.sleep(3000);
webDriver.findElement(By.xpath("//*[text()=\"这是一个弹窗\"]")).click();
Thread.sleep(3000);
webDriver.switchTo().alert().sendKeys("软件测试");
Thread.sleep(3000);
webDriver.switchTo().alert().accept();
}
Title
public static void main(String[] args) {
ChromeOptions options =new ChromeOptions();
options.addArguments("--remote-allow-origins");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("http://localhost:63342/SeleniumTest/page/test5.html?_ijt=klnnrj3i4pn2rhg6cl7a63qibe&_ij_reload=RELOAD_ON_SAVE");
webDriver.findElement(By.cssSelector("input")).sendKeys("E:\\test");
}
quit 关闭了整个浏览器,同时会清空浏览器的cookie,close关闭的是get时获取的页面.
public static void main(String[] args) throws InterruptedException {
ChromeOptions options=new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver=new ChromeDriver(options);
webDriver.get("https://www.baidu.com");
webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
Thread.sleep(3000);
//webDriver.close();
webDriver.quit();
}
public static void main(String[] args) throws InterruptedException, IOException {
WebDriver webDriver=new ChromeDriver();
webDriver.get("Https://www.baidu.com");
webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
webDriver.findElement(By.cssSelector("#su")).click();
Thread.sleep(3000);
File file=((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("E:\\Code\\SeleniumTest\\picture.png"));
}
注:截图操作需要另外引入一个common-io的依赖
Maven Repository: commons-io » commons-io » 2.11.0 (mvnrepository.com)