转:Selenium Webdriver使用技巧(二)
上一篇 / 下一篇 2014-05-05 11:59:57 / 个人分类:Selenium
8.层级定位
层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。
层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。
下面的代码演示了如何使用层级定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的文本
Java代码
importjava.util.List;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxBinary;
importorg.openqa.selenium.firefox.FirefoxDriver;
publicclass LayerLocator {
/**
* @author gongjf
*/
public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.51.com");
//定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的值
WebElement element =driver.findElement(By.className("login"));
List<WebElement> el
=element.findElements(By.tagName("label"));
for(WebElement e : el)
System.out.println(e.getText());
}
}
输出结果:
Java代码
帐号:
密码:
隐身
找到页面元素后,怎样对页面进行操作呢?我们可以根据不同的类型的元素来进行一一说明。
找到输入框元素:
WebElement element =driver.findElement(By.id("passwd-id"));
在输入框中输入内容:
element.sendKeys(“test”);
将输入框清空:
element.clear();
获取输入框的文本内容:
element.getText();
找到下拉选择框的元素:
Select select =
newSelect(driver.findElement(By.id("select")));
选择对应的选择项:
select.selectByVisibleText(“mediaAgencyA”);
或
select.selectByValue(“MA_ID_001”);
不选择对应的选择项:
select.deselectAll();
select.deselectByValue(“MA_ID_001”);
select.deselectByVisibleText(“mediaAgencyA”);
或者获取选择项的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();
对下拉框进行操作时首先要定位到这个下拉框,new一个Selcet对象,然后对它进行操作
找到单选框元素:
WebElement bookMode
=driver.findElement(By.id("BookMode"));
选择某个单选项:
bookMode.click();
清空某个单选项:
bookMode.clear();
判断某个单选项是否已经被选择:
bookMode.isSelected();
多选项的操作和单选的差不多:
WebElement
checkbox=driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();
找到按钮元素:
WebElement saveButton
=driver.findElement(By.id("save"));
点击按钮:
saveButton.click();
判断按钮是否enable:
saveButton.isEnabled ();
也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。例如:
Select lang = new
Select(driver.findElement(By.id("languages")));
lang.selectByVisibleText(“English”);
WebElement
addLanguage=driver.findElement(By.id("addButton"));
addLanguage.click();
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
后面有具体的例子解释~
Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
WebElement approve
=driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只适合于表单的提交
上传文件的元素操作:
WebElement adFileUpload =
driver.findElement(By.id("WAP-upload"));
String filePath
="C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
WebElement
element=driver.findElement(By.name("source"));
WebElement target =
driver.findElement(By.name("target"));
(new
Actions(driver)).dragAndDrop(element,target).perform();
打开一个新的页面:
driver.navigate().to("http://www.example.com");
通过历史导航返回原页面:
driver.navigate().forward();
driver.navigate().back();
有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代码也没有任何问题。这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一。如果你在一个default content中查找一个在iframe中的元素,那肯定是找不到的。反之你在一个iframe中查找另一个iframe元素或default content中的元素,那必然也定位不到。
selenium webdriver中提供了进入一个iframe的方法:
WebDriverorg.openqa.selenium.WebDriver.TargetLocator.frame(String
nameOrId)
也提供了一个返回default
content的方法:
WebDriver
org.openqa.selenium.WebDriver.TargetLocator.defaultContent()
这样使我们面对iframe时可以轻松应对。
以下面的html代码为例,我们看一下处现iframe。
Html代码
main.html
<html>
<head>
<title>FrameTest</title>
</head>
<body>
<divid = "id1">this is a div!</div>
<iframe.
id = "frame" frameborder="0"
scrolling="no"style="left:0;position:absolute;" src
="frame.html"></iframe>
</body>
</html>
frame.html
<html>
<head>
<title>this
is a frame!</title>
</head>
<body>
<divid = "div1">this is a div,too!</div>
<label>input:</label>
<inputid = "input1"></input>
</body>
</html>
Java代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class FameStudy {
publicstatic void main(String[] args) {
WebDriverdr = new FirefoxDriver();
Stringurl = "\\Your\\Path\\to\\main.html";
dr.get(url);
//在defaultcontent定位id="id1"的div
dr.findElement(By.id("id1"));
//此时,没有进入到id="frame"的frame中时,以下两句会报错
dr.findElement(By.id("div1"));//报错
dr.findElement(By.id("input1"));//报错
//进入id="frame"的frame中,定位id="div1"的div和id="input1"的输入框。
dr.switchTo().frame("frame");
dr.findElement(By.id("div1"));
dr.findElement(By.id("input1"));
//此时,没有跳出frame,如果定位defaultcontent中的元素也会报错。
dr.findElement(By.id("id1"));//报错
//跳出frame,进入defaultcontent;重新定位id="id1"的div
dr.switchTo().defaultContent();
dr.findElement(By.id("id1"));
}
}
小结:
switch_to方法会new1个TargetLocator对象,使用该对象的frame方法可以将当前识别的”主体”移动到需要定位的frame上去。
在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id、name的时候。在selenium webdriver中得到新开窗口相对简单的多,它无关新开窗口的id、name等属性。以下面的html为例:
Html代码
<span style="white-space:
normal;background-color: #ffffff;">test.html</span>
<html>
<head><title>Test
Popup Window</title></head>
<body>
<a
id = "51" href = "http://www.51.com/" target
="_blank">Let's go!</a>
</body>
</html>
下面的代码演示了如何去得到弹出的新窗口
Java代码
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class PopupWindowTest {
/**
* @author gongjf
*/
publicstatic void main(String[] args) {
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla
Firefox\\firefox.exe");
WebDriverdr = new FirefoxDriver();
Stringurl ="\\Your\\Path\\to\\main.html";
dr.get(url);
dr.findElement(By.id("51")).click();
//得到当前窗口的句柄
StringcurrentWindow = dr.getWindowHandle();
//得到所有窗口的句柄
Set<String>handles = dr.getWindowHandles();
Iterator<String>it = handles.iterator();
while(it.hasNext()){
if(currentWindow== it.next()) continue;
dr.switchTo().window(it.next());
}
}
}
输出结果:
title,url = 51.com真人配对玩游戏,http://www.51.com/
小结:
捕获或者说定位弹出窗口的关键在于获得弹出窗口的句柄。(
在上面的代码里,使用windowhandle方法来获取当前浏览器窗口的句柄,使用了windowhandles方法获取所有弹出的浏览器窗口的句柄,然后通过排除当前句柄的方法来得到新开窗口的句柄。
在获取新弹出窗口的句柄后,使用switchto.window(newwindow_handle)方法,将新窗口的句柄作为参数传入既可捕获到新窗口了。
如果想回到以前的窗口定位元素,那么再调用1次switchto.window方法,传入之前窗口的句柄既可达到目的。
TAG:
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 430044
- 日志数: 133
- 建立时间: 2013-07-02
- 更新时间: 2014-09-17