I do not see HTML for all 6 numbers.
But for this HTML
<span class="number" data-to="905">905</span>
</div>
<p class="title">Animals Placed</p>
Your script should look something like this :
XPath
//p[text()='Animals Placed']/preceding-sibling::div/span[@class='number']
Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL + F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
Code trial 1:
time.sleep(5)
animal_num = driver.find_element_by_xpath("//p[text()='Animals Placed']/preceding-sibling::div/span[@class='number']").text
print(animal_num)
Code trial 2:
animal_num = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[text()='Animals Placed']/preceding-sibling::div/span[@class='number']"))).text
print(animal_num)
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update:
Please use the below xpath
//span[@class='number' and @data-to]
it should represent all the number of nodes in HTML DOM.
driver.maximize_window()
driver.get("https://tulsaspca.org/")
driver.execute_script("window.scrollTo(0, 250)")
all_numbers = driver.find_elements(By.XPATH, "//span[@class='number' and @data-to]")
for number in all_numbers:
print(number.text)
Output :
905
4896
7105
23194
1004
42000