1

I'm trying to print the text for a table cell, but nothing is being printed. It's identifying the element, but doesn't seem to be able to locate the text.

The HTML looks like this:

<tr style="font-weight: 600;">
  <td>
      "
    Available Balance
                                       "
  </td>
  <td id="testBalance" class="text-right ng-binding" style="font-weight:   600;">

                                                         664,265.314
  </td>                                                        

I'm trying to read (and print) the value of "664,256.314" by running:

Balance=driver.find_element_by_id("testBalance")
print (Balance.text)

It locates the element, but nothing gets printed. Does anyone know why this is happening?

4
  • By convention Python variables should start with a lowercase. As for the problem, I tried it using the html you've provided and it worked fine for me. Commented Jun 23, 2016 at 13:36
  • I still can't get it to print. I forgot to put the double quotes in my html example (it looks the same as Available Balance in my example -- " 664,265.314 ". Would the double quotes or the spaces between the double quotes and the number make a difference? Commented Jun 23, 2016 at 13:51
  • 1
    I doubt it. You could try using get_attribute('textContent') and see what that gives you. Commented Jun 23, 2016 at 13:53
  • That worked for me. Thanks for the help, much appreciated. Commented Jun 23, 2016 at 14:31

2 Answers 2

1

I think it's just a timing issue - the balance has not been set/updated at the moment you retrieve it's value. Add an Explicit Wait to wait for the text to be present:

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

class wait_for_any_text(object):
    def __init__(self, locator, text_):
        self.locator = locator
        self.text = text_

    def __call__(self, driver):
        try:
            element_text = EC._find_element(driver, self.locator).text
            return element_text.strip()
        except StaleElementReferenceException:
            return False

wait = WebDriverWait(driver, 10)
balance = wait.until(wait_for_any_text((By.ID, "testBalance"))) 

print(balance.text)
Sign up to request clarification or add additional context in comments.

Comments

1

(Thanks to RemcoW)

print(Balance.get_attribute('textContent'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.