- 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.
推薦閱讀
- pcDuino開發實戰
- 阿里云數字新基建系列:云原生操作系統Kubernetes
- 高性能Linux服務器構建實戰:系統安全、故障排查、自動化運維與集群架構
- Instant Optimizing Embedded Systems using Busybox
- 移動應用UI設計模式(第2版)
- Joomla! 3 Template Essentials
- Linux內核觀測技術BPF
- RHCSARHCE 紅帽Linux認證學習指南(第7版)EX200 & EX300
- 寫給架構師的Linux實踐:設計并實現基于Linux的IT解決方案
- 從實踐中學習Windows滲透測試
- 統信UOS應用開發進階教程
- 大規模分布式系統架構與設計實戰
- Learn Quantum Computing with Python and IBM Quantum Experience
- Docker容器技術與運維
- OpenSolaris系統管理