@Test
public void testUntitledTestCase() throws Exception {
driver.get("file:///C:/Users/a/Desktop/select1.html");
WebElement div=driver.findElement(By.id("oo"));
List<WebElement> rows=div.findElements(By.tagName("li"));
System.out.println(rows.size()); //3 限定範圍在id=oo的div中
}
@Test
public void testUntitledTestCase() throws Exception {
driver.get("file:///C:/Users/a/Desktop/select1.html");
List<WebElement> rows=driver.findElements(By.tagName("li"));
System.out.println(rows.size());//6 範圍在整個頁面
}
WebDriver.findElement()和WebElement.findElement()之間的區別:
WebDriver.findElement():是整個頁面中根據條件篩選元素
WebElement.findElement():是從某個元素的區域內根據條件篩選元素
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Checkbox</title>
</head>
<body>
<h3>table</h3>
<div id="oo">
<ul>
<li>
<h3>aa</h3>
</li>
<li>
<h3>bb </h3>
</li>
<li>
<h3>cc </h3>
</li>
</ul>
</div>
<div id="qq">
<ul>
<li>
<h3>aa</h3>
</li>
<li>
<h3>bb </h3>
</li>
<li>
<h3>cc </h3>
</li>
</ul>
</div>
</body>