Count the number of elements found in selenium python

I have the following html page where I try to find a word silverand keep track of how many are found.

These are just two shown here, but it can generate more, so I don’t know the exact quantity.

<tr id="au_4" class="odd">
<td>Silver</td>
</tr>
<tr id="au_4" class="even">
<td>Silver</td>
</tr>

This is what I tried but no luck:

count =  driver.find_elements_by_xpath("//td[text()='Silver']")
+4
source share
2 answers

count- a list of all found items. To find its length, you must:

len(count)

I highly recommend you go through docs to better understand how Selenium works.

+14
source

execute_script, len find_elements_by :

script = "return $(\"td:contains('{}')\").length".format(text="Silver")
count = driver.execute_script(script)

jQuery, innerText SO...

0

Source: https://habr.com/ru/post/1570068/


All Articles