八、定位页面元素

webdriver提供了强大的元素定位方法,支持以下三种方法。
[color=red]单个对象的定位方法
多个对象的定位方法
层级定位[/color]
注意:
selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素 List,如果不存在符合条件的就返回一个空的list。
[b][size=large]1.定位单个对象[/size][/b]
webdriver使用了以下方法定位元素:

* By.className(className))
* By.cssSelector(selector)
* By.id(id)
* By.linkText(linkText)
* By.name(name)
* By.partialLinkText(linkText)
* By.tagName(name)
* By.xpath(xpathExpression)
[b]1)使用className定位元素[/b]
定位的html文件:

<form action="/s" name="f">
<span class="s_ipt_wr">
<input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">
</span>
<input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">
<span class="s_btn_wr">
<input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">
</span>
<div id="sd_1350115810720" style="display: none;"></div>
</form>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据className定位百度查询输入框
WebElement search=driver.findElement(By.className("s_ipt"));
//输入查询条件classname
search.sendKeys("classname");
}

}

[b]2)使用cssSelector定位元素[/b]
定位的html文件:

<form action="/s" name="f">
<span class="s_ipt_wr">
<input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">
</span>
<input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">
<span class="s_btn_wr">
<input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">
</span>
<div id="sd_1350115810720" style="display: none;"></div>
</form>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据classSelector定位百度查询输入框
WebElement search=driver.findElement(By.cssSelector("#kw"));
//输入查询条件classname
search.sendKeys("classname");
}

}

[b]3)使用id定位元素[/b]
定位的html文件:

<form action="/s" name="f">
<span class="s_ipt_wr">
<input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">
</span>
<input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">
<span class="s_btn_wr">
<input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">
</span>
<div id="sd_1350115810720" style="display: none;"></div>
</form>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据id定位百度查询输入框
WebElement search=driver.findElement(By.id("kw"));
//输入查询条件classname
search.sendKeys("classname");
}

}

[b]4)使用name定位元素[/b]
定位的html文件:

<form action="/s" name="f">
<span class="s_ipt_wr">
<input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">
</span>
<input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">
<span class="s_btn_wr">
<input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">
</span>
<div id="sd_1350115810720" style="display: none;"></div>
</form>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据name定位百度查询输入框
WebElement search=driver.findElement(By.name("wd"));
//输入查询条件classname
search.sendKeys("classname");
}

}

[b]5)使用tagname定位元素[/b]
定位的html文件:

<form action="/s" name="f">
<span class="s_ipt_wr">
<input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">
</span>
<input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">
<span class="s_btn_wr">
<input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">
</span>
<div id="sd_1350115810720" style="display: none;"></div>
</form>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据tagName定位百度查询输入框
WebElement search=driver.findElement(By.tagName("input"));
//输入查询条件classname
search.sendKeys("classname");
}

}

[b]6)使用xpath定位元素[/b]
定位的html文件:

<form action="/s" name="f">
<span class="s_ipt_wr">
<input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">
</span>
<input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">
<span class="s_btn_wr">
<input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">
</span>
<div id="sd_1350115810720" style="display: none;"></div>
</form>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据xpath定位百度查询输入框
WebElement search=driver.findElement(By.xpath("//*[@id='kw']"));
//输入查询条件classname
search.sendKeys("classname");
}

}

[b]7)使用linkText定位元素[/b]
定位的html文件:

<a href="http://baike.baidu.com" name="tj_baike">百科</a>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据linkText定位
WebElement link=driver.findElement(By.linkText("百科"));
link.click();
}

}

[b]8)使用partialLinkText定位元素[/b]
定位的html文件:

<a href="http://baike.baidu.com" name="tj_baike">百科</a>


package selenium.test.googleSearch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//根据linkText定位
WebElement link=driver.findElement(By.linkText("科"));
link.click();
}

}

[b][size=large]2.定位多个对象[/size][/b]

package selenium.test.googleSearch;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//定位百度快捷功能链接,并返回其值
List<WebElement> links=driver.findElements(By.id("nv"));
for(WebElement e:links){
System.out.println(e.getText());
}
}

}

其输出结果为:

新 闻 网 页 贴 吧 知 道 MP3 图 片 视 频 地 图

[b][size=large]3.层级定位[/size][/b]
层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。
层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。
定位的html文件:

<p id="nv">
<a href="http://news.baidu.com" name="tj_news">新 闻</a>
<b>网 页</b>
<a href="http://tieba.baidu.com" name="tj_tieba">贴 吧</a>
<a href="http://zhidao.baidu.com" name="tj_zhidao">知 道</a>
<a href="http://mp3.baidu.com" name="tj_mp3">MP3</a>
<a href="http://image.baidu.com" name="tj_img">图 片</a>
<a href="http://video.baidu.com" name="tj_video">视 频</a>
<a href="http://map.baidu.com" name="tj_map">地 图</a>
</p>


package selenium.test.googleSearch;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//先定位父元素nv
WebElement p=driver.findElement(By.id("nv"));
//再根据父元素定位子元素
WebElement link=p.findElement(By.name("tj_zhidao"));
//打印子元素的link值
System.out.println(link.getText());

}

}

输出结果为:
知 道
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值