Using Selenium and Python, but Print function not working

Question:

I am using selenium to write a test script to purchase a number of items automatically. However for some reason when I am asking python to print a certain elements text, nothing is appearing in the console for me to assert that my script has selected the correct colour of the item.

# Driver select the first trainer option visible on the page
driver.find_element(By.XPATH,"//img[@title='Fresh Foam X 1080v12, M1080Z12']").click()
# Driver click on the orange version of these trainers
driver.find_element(By.XPATH,"//button[contains(@title,'M1080M12')]//span[contains(@class,'p-auto')]").click()
# Make sure you have the correct colour
trainerColour1 = driver.find_element(By.XPATH,"//span[@class='display-color-name color-name-mobile font-body regular pdp-update-event-triggerd']").text
print (trainerColour1) # For some reason its not printing this element on the log
#assert "apricot" in trainerColour1

At this point I expected "Vibrant orange with spring tide and vibrant apricot" to appear on the console log and for me to assert the word "apricot" to make sure the correct colour had been selected. However nothing is appearing on the console.Console Result

Asked By: Aristarchus24

||

Answers:

If the text is not on the element and is on childrens of the element, change .text to .get_attribute("innerText")

Other solution could be accurate more on the element you want to extract, but sometimes you need this cause text is mixed on multiple elements

trainerColour1 = driver.find_element(By.XPATH,"//span[@class='display-color-name color-name-mobile font-body regular pdp-update-event-triggerd']").get_attribute("innerText") 
Answered By: Wonka