八种定位方式
一、By.id(id):通过ID 属性查找
HTML 源码
<a onclick="return false;" id="lb" name="tj_login" href="https://passport.baidu.com/v2/?login&tpl=mn&">登录</a>
Java代码
WebElement element = driver.findElement(By. id("lb"));
二、By.name(name):通过name属性查找
HTML 源码
<a onclick="return false;" id="lb" name="tj_login" href="https://passport.baidu.com/v2/?login&">登录</a>
java代码
WebElement element=driver.findElement(By.name("tj_login"));
三、By.className(className) :通过class属性查找
HTML 源码
<a class="reg" href="https://passport.baidu.com/v2/?reg&u=http%3A%2F%2Fwww.baidu.com%2F">注册</a>
java代码
WebElement element = driver.findElement(By.className( "reg"));
四、By.linkText(链接文本):通过链接文本
HTML 源码
<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>
java代码
WebElement element = driver.findElement(By.linkText( "搜索设置" ));
五、By.partialLinkText(部分链接文本):通过部分链接文本
HTML 源码
<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>
Java代码
WebElement element = driver.findElement(By. partialLinkText( "搜索" ));
六、By.cssSelector(Css路径):通过CSS路径
HTML源码
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
Java代码
WebElement element=driver.findElement(By.cssSelector("#su"));
七、By.tagName(name):通过tagname查找
HTML 源码
<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>
Java代码
WebElement element=driver.findElement(By.tagName("a"));
八、By.xpath(XPath路径):通过XPath查找
HTML源码
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
Java代码
WebElement element=driver.findElement(By. xpath( ".//*[@id='kw']"));
操作元素
一、点击元素:click()
java代码
WebElement baiduKW=driver.findElement(By.cssSelector("#su"));
baiduKW.click();
二、文本框输入文本:sendkeys()
java代码
WebElement baiduKw=driver.findElement(By.id("kw"));
baiduKw.sendKeys("selenium");
三、清空文本框:clear()
java代码
WebElement baiduKw=driver.findElement(By.id("kw")); baiduKw.sendkeys("selenium"); baiduKw.clear();
四、获取文本:getText()
java代码
//定位百度首页右上角的 新闻 WebElement XW=driver.findElement(By.xpath("//*[@name='tj_trnews']")); //获取文本 String Text1=XW.getText(); //校验文本内容 Assert.assertEquals(Text1,"新闻");
五、判断元素是否展示:isDisplayed()
java代码
WebElement baiduSu=driver.findElement(By.xpath("//*[@id='su']")); //获取 百度一下 按钮是否显示 boolean Su=baiduSu.isDisplayed(); //校验结果 Assert.assertTrue(Su);
六、判断选择框是否选取:isSelected()
HTML源码:
<td>RadioBox单选框</td> <td class="widgetStyle"> <div id='radio'> <input type='radio' name="identity" class='a'/><label>a</label></br> <input type='radio' name="identity" class='b'/><label>b</label></br> <input type='radio' name="identity" class='c'/><label>c</label></br> <input type='radio' name="identity" class='d'/><label>d</label> </div> </td>
java代码:
//定位a单选框 WebElement a=driver.findElement(By.xpath("//*[@id='radio']/input[1]")); //点击单选框 Vo.click(); //获取单选框是否被选中 boolean b1=Vo.isSelected(); //校验结果 Assert.assertTrue(b1);
七、判断元素是否激活:isEnabled()
HTML源码:
<td>Button按钮</td> <td class="SubButton">
<div id='button'> <input name="buttonSub" type='button' class='button' value='Submit' disabled="disabled"/> </div> </td>
Java代码:
//定位元素,判断是否激活 boolean submit=driver.findElement(By.xpath("//*[@name='buttonhtml']")).isEnabled(); //校验结果 Assert.assertTrue(submit);
八、alert弹窗
HTML源码:
<input id='test' type='button' onclick="alert('这是一个alert弹框');" value='单击此按钮,弹出alert弹窗'>
java代码:
//定位Alert弹窗 WebElement At=driver.findElement(By.id("test")); //点击alert按钮 At.click(); //选择alert弹窗 Alert alert=driver.switchTo().alert(); //获取alert弹窗中的文本内容 String text=alert.getText(); //校验alert弹窗中的文本 Assert.assertEquals(text,"这是一个alert弹框"); //点击alert弹窗中的确定按钮 alert.accept();
九、Confirm弹窗
HTML源码:
<input id='testcf' type='button' onclick="confirm('这是一个confirm弹框');" value='单击此按钮,弹出confirm弹窗'></input>
java代码:
//定位confirm按钮 WebElement Cf=driver.findElement(By.xpath("//*[@id='testcf']")); Cf.click(); //选取alert弹窗 Alert confirm=driver.switchTo().alert(); //点击confirm弹窗上的确定按钮 confirm.accept(); //点击confirm弹窗上面的取消按钮 //confirm.dismiss();
十、prompt弹窗
HTML源码
<input id='testpt' type='button' onclick="prompt('这是一个prompt弹框');" value='单击此按钮,弹出prompt弹窗'></input>
Java代码
//定位prompt按钮 WebElement Cf=driver.findElement(By.xpath("//*[@id='testpt']")); Cf.click(); //选取alert弹窗 Alert prompt=driver.switchTo().alert(); //在prompt弹窗的输入框中输入文字 prompt.sendKeys("在prompt弹窗中的输入框中输入123za。。。"); Thread.sleep(3000); //点击prompt弹窗中的确定按钮 prompt.accept(); //点击Prompt弹窗中的取消按钮 //prompt.dismiss();
十一、iFrame
HTML源码
<html> <meta http-equiv=Content-Type content="text/html;charset=utf-8"> <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1"> <meta content=always name=referrer> <head> <title>selenium</title> </head> <body> <input id='test' type='button' onclick="alert('这是一个alert弹框');" value='单击此按钮,弹出alert弹窗'></input> <input id='testcf' type='button' onclick="confirm('这是一个confirm弹框');" value='单击此按钮,弹出confirm弹窗'></input> <input id='testpt' type='button' onclick="prompt('这是一个prompt弹框');" value='单击此按钮,弹出prompt弹窗'></input> <br> <td>Input输入框</td> <div id='ifra'><input type="text" id="input"/></div> </body> <br> <td>Iframe框架</td> <iframe width=700 height=150 name=iFrame frameborder=0 src="selenium.html"></iframe> </html>
java代码
//用iframe标签的id或者name定位iframe,并将控制权交给iframe driver.switchTo().frame("iFrame"); //定位iframe里面的输入框并输入123 driver.findElement(By.id("input")).sendKeys("123"); //控制权交回原来的界面 driver.switchTo().defaultContent(); //在外面的输入框中输入123 driver.findElement(By.id("input")).sendKeys("123");
当iframe没有id和name可用时可以使用index、WebElement方法(还没弄明白index方法暂时不写出来了)
HTML源码
java代码
WebElement e = driver.findElement(By.xpath("//div[@id='loginDiv']/iframe[@frameborder='0']"));
driver.switchTo().frame(e);
十二、select下拉框处理
1. selectByIndex() 根据索引来选取,从0开始
2. selectByValue() 根据属性value的属性值来选取
3. selectByVisibleText()根据标签之间的Text值,也就是页面显示的
HTML源码
<tr> <td>Select下拉列表</td> <div id='select'> <select id="selected"> <option value ="a">huawei</option> <option value ="b">oppo</option> <option value="c">vivo</option> <option value="d">xiaomi</option> <option value="e">hongmi</option> <option value="f">meizu</option> </select> </div> </tr>
Java代码
WebElement st=driver.findElement(By.id("selected")); //new一个Select对象 Select select=new Select(st); //通过索引选择,从0开始 select.selectByIndex(1); //通过value值选择 select.selectByValue("c"); //通过文本选择 select.selectByVisibleText("xiaomi");
十三、多窗口处理
java代码
//定位点击元素,打开新的窗口 driver.findElement(By.className("open")).click(); //获取当前页面句柄 String handle=driver.getWindowHandle(); //获取所有页面句柄,并循环判断不是当前的句柄就做选取switchTo() for(String handles:driver.getWindowHandles()){ if(handles.equals(handle)) continue;//跳过本次循环,不执行下面的命令直接进行下一次循环 //break; //结束全部for循环 driver.switchTo().window(handles); } //在新打开的页面中进行操作 driver.findElement(By.id("input")).sendKeys("新页面"); //关闭新页面 driver.close(); //选择原来的句柄,切到原来的界面 driver.switchTo().window(handle); //在原来的界面进行操作 driver.findElement(By.id("input")).sendKeys("旧页面");