這里從我們team的代碼中來(lái)總結(jié)下常見(jiàn)的幾種找頁(yè)面元素的方法:
(1)通過(guò)WebElement的ID
如果某個(gè)WebElement提供了ID,
<input type="text" name="passwd"id="passwd-id" />
(2)通過(guò)WebElement的name查找:
WebElement element = driver.findElement(By.name("passwd"));
(3)通過(guò)WebElement的xpath查找:
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
(4)通過(guò)WebElement的樣式查找:
<div class="cheese"><span>Cheddar</span></div><divclass="cheese"><span>Gouda</span></div>
可以通過(guò)這樣查找頁(yè)面元素:
List<WebElement>cheeses = driver.findElements(By.className("cheese"));
(5)通過(guò)超鏈接文本查找:
<ahref="http://www.google.com/search?q=cheese">cheese</a>>
那么可以通過(guò)這樣查找:
WebElement cheese =driver.findElement(By.linkText("cheese"));