- Selenium WebDriver Quick Start Guide
- Pinakin Chaubal
- 178字
- 2021-06-24 18:26:28
SearchContext interface
SearchContext is the interface that has been designed to find elements in the DOM using a locator mechanism. SearchContext has two public methods:
- findElement: This identifies all matching elements based on a locator mechanism but always returns the first matching web element.
- findElements: This returns a list of WebElements that match the locator.
When Selenium returns a WebElement, it does not return an object of the WebElement, since the WebElement is an interface. Rather, it returns an object of RemoteWebElement, which is the implementation class of the WebElement interface.
What if the locator mechanism does not return any elements? In this case, findElement will always throw a NoSuchElementException. The findElements method, on the other hand, will not throw a No SuchElementException. It returns an empty list.
Always use findElements when unsure about the existence of a WebElement based on some locator mechanism. This is the best practice to avoid NoSuchElementException at runtime. It is very likely you'll get this exception while using the By.tagName static method, which we will see later.
推薦閱讀
- Linux Mint Essentials
- Extending Puppet
- 構建可擴展分布式系統:方法與實踐
- Linux集群和自動化運維
- 混沌工程:復雜系統韌性實現之道
- 竹林蹊徑:深入淺出windows驅動開發
- 嵌入式系統原理及開發
- 蘋果OS X Mavericks 10.9應用大全
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- Cassandra 3.x High Availability(Second Edition)
- 分布式系統設計實踐
- UI設計手繪表現從入門到精通
- Linux應用大全 基礎與管理
- VMware Horizon Mirage Essentials
- Azure Resource Manager Templates Quick Start Guide