Xpath : 使用部分属性值模糊匹配来定位页面元素
2016-07-25 16:05
1021 查看
//定位以 “ http://v ” 开头的link
List<WebElement> startLink = driver.findElements(By.xpath("//a[starts-with(@href,'http://v')]"));
//定位id含有 “ i ” link
List<WebElement> containLink = driver.findElements(By.xpath("//a[contains(@id,'i')]"));许多文档上有end-with的API,但本人使用该函数提示为Invalid,尚未测试成功,故不写入。总结:
package demo.test;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.framework.webdriver.DriverFactory;
public class FindByXpath {
WebDriver driver;
@BeforeClass
public void beforeClass(){
this.driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
}
@Test
public void getElementByXpath(){
try {
//定位以 “ http://v ” 开头的link
List<WebElement> startLink = driver.findElements(By.xpath("//a[starts-with(@href,'http://v')]"));
System.out.println("startLinkSize = "+startLink.size());
//定位id含有 “ i ” link
List<WebElement> containLink = driver.findElements(By.xpath("//a[contains(@id,'i')]"));
System.out.println("containLinkSize = "+containLink.size());
} catch (Exception e) {
System.out.println(e);
}
}
}运行结果:
相关文章推荐
- C#通过xpath查找xml指定元素的方法
- xpath的数据和节点类型以及XPath中节点匹配的基本方法
- c#通过xpath读取xml示例
- JQuery 选择器 xpath 语法应用
- 深入XPath的详解以及Java示例代码分析
- htmlcleaner使用方法及xpath语法初探
- 简单了解JavaScript操作XPath的一些基本方法
- java使用xpath和dom4j解析xml
- XPath解析属性、节点内容及遍历子节点
- 简单了解JavaScript操作XPath的一些基本方法
- libxml2的Xpath使用心得
- Python etree.xpath不能准确定位HTML
- scrapy测试xpath
- C#用xpath查找某节点
- python scrapy学习踩点
- Xpath
- Dom4j和Xpath
- 利用Dom4j+xpath读取XML文档
- Schema之key、keyref