Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of elements found using Selenium WebDriver with Python?

I looked at the documentation located here, but couldn't find an answer.

I want to get an element by class name or xpath and return the number of instances. There seems to be no available function in Python, such as get_xpath_count().

Any ideas on how to achieve this?

like image 772
samxli Avatar asked Dec 14 '11 12:12

samxli


2 Answers

Try driver.find_elements_by_xpath and count the number of returned elements.

like image 73
Sam Avatar answered Nov 09 '22 13:11

Sam


You Can simply use len() function:

len(driver.find_elements_by_xpath('//a'))
like image 20
Nima Soroush Avatar answered Nov 09 '22 13:11

Nima Soroush