Search code examples
pythonselenium-webdriverselenium-chromedriver

Selenium Identifying Text as Empty Array


I have a python selenium script and I am trying to grab the inventory from Carmax.com as shown in the first screenshot. However, I am getting an empty array [] returned from my print statement.

enter image description here

Here is the html. enter image description here

Another HTML example:

<div class="number-of-matches">
    <span class="header-value hzn-typography--body-1">45,475</span>
        <span class="header-label"> Matches</span></div>

My code results in just having an empty array [] returned, and not the 44,015 (or 45,475 in second HTML example) I expected.

import socket
import os
import json
import certifi
import ssl
import certifi
import smtplib
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver=webdriver.Chrome()
driver.get("https://www.carmax.com")
time.sleep(8)


try:
    if driver.find_element(By.ID, "header-search-button"):
        driver.find_element(By.ID, "header-search-button").click()
        time.sleep(25)             
        inventory = driver.find_elements(By.CLASS_NAME, 'header-value hzn-typography--body-1')
        print(inventory)
            
except:
    time.sleep(3)

Not sure what to do here. It is not like this is hidden text right? Totally visible on the page.


Solution

  • Just use XPATH specifying the type of tag and its class, like this:

    inventory = driver.find_element(By.XPATH, 
    '//span[@class="header-value hzn-typography--body-1"]').text        
    print(inventory)
    

    the output

    45,223