How to Count Elements#
Sometimes it's useful to count the elements of a specific selector, either to check whether the expected number of elements are present, or to automate an iteration over them.
Use Case for Counting Search Results#
For instance, you may want to know how many search results are displayed on a search engine result page:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Other Examples#
Images#
How to count the number of <img>
image elements on a page:
number_of_images = browser.tool.count_elements("//img")
Links#
How to count the number of <a>
link elements on a page:
number_of_links = browser.tool.count_elements("//a")
Headlines#
How to count the number of <h1>
headline elements on a page:
number_of_headlines = browser.tool.count_elements("//h1")
Paragraphs#
How to count the number of <p>
paragraph elements on a page:
number_of_paragraphs = browser.tool.count_elements("//p")