0

I'm trying to write a script that will grab the following 6 values from the website tulsaspca.org and print them in a .txt file.

6 Values

The final output should be:

905 
4896
7105
23194
1004
42000

HTML for "Animals Placed"

<span class="number" data-to="905">905</span>
</div>
<p class="title">Animals Placed</p>

I wrote the following code, but it doesn't seem to be working.

for element in driver.find_elements_by_class_name('Animals Placed'):
  print(element.text)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
zackchess1
  • 65
  • 1
  • 7

2 Answers2

0

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
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

To grab the six values from the website TULSASPCA and print them in a text file you need to induce WebDriverWait for the visibility_of_all_elements_located() and then using List Comprehension you can create a list and subsequently create a DataFrame and finally export the values to a TEXT file excluding the Index using the following Locator Strategies:

Code Block:

driver.get("https://tulsaspca.org/")
driver.execute_script("window.scrollTo(0, 250)")
# read into a DataFrame
df = pd.DataFrame([my_elem.get_attribute("data-to") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='number']")))])
# Exporting as TEXT file excluding the Index
df.to_csv("C:\\Data_Files\\output_files\\new_text_marks.txt", index=False)
driver.quit()

Snapshot of Output Text file:

panda_output_text

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

PS: You may like to drop the first row from the DataFrame

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352