查找条件对象
16.5.1 Id(idToFind)按html 元素的id属性查找元素
driver.get("file:///C:/exam/House.html");
WebElement inputKey=driver.findElement(By.id("email"));
inputKey.clear();
inputKey.sendKeys("test@test.com");
16.5.2Name(nameToFind)
按html 元素的name属性查找元素
driver.get("file:///C:/exam/House.html");
WebElement inputKey=driver.findElement(By.name("username"));
inputKey.clear();
inputKey.sendKeys("test");
16.5.3LinkText(linkTextToFind)
按html 元素的按链接的文本进行查找查找元素
driver.get("file:///C:/exam/index.html");
WebElement clickLink=driver.findElement(By.linkText("继续提供房源信息"));
Thread.sleep(3);
clickLink.click();
16.5.4partialLinkText
按html 元素的按链接的文本进行进行模糊查找元素
driver.get("file:///C:/exam/index.html");
WebElement clickLink=driver.findElement(By.partialLinkText("提供房源"));
Thread.sleep(3);
clickLink.click();
16.5.5className(classzNameToFind)
按元素的class 属性进行查找
driver.get("file:///C:/exam/House.html");
WebElement inputBox=driver.findElement(By.className("uname"));
inputBox.clear();
inputBox.sendKeys("test");
16.5.6TagName(TagNameToFind)
按标记名称查找
driver.get("file:///C:/exam/index.html");
WebElement linkText=driver.findElement(By.tagName("a"));
linkText.click();
实例: testlist 代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<table border=1 cellspacing="0">
<tr>
<td>姓名</td>
<td>代码基础</td>
<td>测试理论</td>
<td>测试工具</td>
</tr>
<tr>
<td>史宝宝</td>
<td>90</td>
<td>98</td>
<td>88</td>
</tr>
<tr>
<td>吴迪</td>
<td>88</td>
<td>90</td>
<td>98</td>
</tr>
<tr>
<td>徐亮亮</td>
<td>77</td>
<td>88</td>
<td>99</td>
</tr>
<tr>
<td>涛涛</td>
<td>89</td>
<td>79</td>
<td>99</td>
</tr>
</table>
</body>
</html>
driver.get("http://192.168.2.160/test/testlist.html");
WebElement table=driver.findElement(By.xpath("//table"));
List<WebElement> rows=table.findElements(By.tagName("tr"));
for(WebElement row:rows){
List<WebElement> cols=row.findElements(By.tagName("td"));
for(WebElement col:cols){
System.out.print(col.getText()+" ");
}
System.out.println();
16.5.7XPath(xPathToFind)
driver.get("file:///C:/exam/House.html");
WebElement inputBox=driver.findElement(By.xpath("//input[@name='username']"));
inputBox.clear();
inputBox.sendKeys("test");
16.5.8cssSelect
driver.get("file:///C:/exam/House.html");
WebElement inputBox=driver.findElement(By.cssSelector("input.uname"));
inputBox.clear();
inputBox.sendKeys("test"); 咱可以加点文字说明就更好了
页:
[1]