八、定位页面元素

本文介绍了Selenium WebDriver中常见的元素定位方法,包括单个对象定位如className、cssSelector、id、name、tagName、xpath等,以及多个对象定位和层级定位方法。通过实例展示了如何在百度网页中使用这些定位技巧。
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());

}

}

输出结果为:
知 道
【电能质量扰动】基于ML和DWT的电能质量扰动分类方法研究(Matlab实现)内容概要:本文研究了一种基于机器学习(ML)和离散小波变换(DWT)的电能质量扰动分类方法,并提供了Matlab实现方案。首先利用DWT对电能质量信号进行多尺度分解,提取信号的时频域特征,有效捕捉电压暂降、暂升、中断、谐波、闪变等常见扰动的关键信息;随后结合机器学习分类器(如SVM、BP神经网络等)对提取的特征进行训练与分类,实现对不同类型扰动的自动识别与准确区分。该方法充分发挥DWT在信号去噪与特征提取方面的优势,结合ML强大的模式识别能力,提升了分类精度与鲁棒性,具有较强的实用价值。; 适合人群:电气工程、自动化、电力系统及其自动化等相关专业的研究生、科研人员及从事电能质量监测与分析的工程技术人员;具备一定的信号处理基础和Matlab编程能力者更佳。; 使用场景及目标:①应用于智能电网中的电能质量在线监测系统,实现扰动类型的自动识别;②作为高校或科研机构在信号处理、模式识别、电力系统分析等课程的教学案例或科研实验平台;③目标是提高电能质量扰动分类的准确性与效率,为后续的电能治理与设备保护提供决策依据。; 阅读建议:建议读者结合Matlab代码深入理解DWT的实现过程与特征提取步骤,重点关注小波基选择、分解层数设定及特征向量构造对分类性能的影响,并尝试对比不同机器学习模型的分类效果,以全面掌握该方法的核心技术要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值