ChrisG29
ChrisG29

Reputation: 1571

Get text from a label using Selenium (Python)

I'm trying to read the text from a label that changes according to certain conditions (eg. if you enter a Username already in use, the label will display "Username already in use").

I've been trying to read in the text from the label that gets displayed, but nothing I've tried has worked so far.

The HTML looks like this:

 <div class="margin-top-10">
     <span ng-show="sentValidation">
         <span id="test1" ng-show="userNameAvailable" class="txt-color-green">
             <i class="fa fa-check fa-lg"></i>Username available
         </span>
         <span id="test2" ng-show="!userNameAvailable && userNameAvailable != null" class="txt-color-red">
                <i class="fa fa-exclamation-circle"></i>Username unavailable
          </span>
     </span>
     <div class="txt-color-red" ng-show="form.cUsername.$dirty && form.cUsername.$invalid">
          <p id="test3" ng-show="form.cUsername.$error.required">
              <i class="fa fa-exclamation-circle"></i>Username is required
          </p>
          <p id="test4" class="txt-color-red" ng-show="form.cUsername.$error.maxlength">
             <i class="fa fa-exclamation-circle"></i>Maxmium length is 50 characters
          </p>
          <p id="test5" class="txt-color-red" ng-show="form.cUsername.$error.minlength">
               <i class="fa fa-exclamation-circle"></i>Minimum length is 4 characters
           </p>
     </div>
</div>

Does anyone know how I can read what gets displayed on the screen?

Upvotes: 0

Views: 2047

Answers (3)

Mohammad Shahid Siddiqui
Mohammad Shahid Siddiqui

Reputation: 4190

content = driver.find_element_by_class_name('fa-exclamation-circle')
print (content.text)

Upvotes: 1

omri_saadon
omri_saadon

Reputation: 10669

content = driver.find_element_by_class_name('fa-exclamation-circle')
content.is_displayed()

You can use the find_element_by_class_name built in method.

You need to pass the class name as a parameter.

Upvotes: 0

ChrisG29
ChrisG29

Reputation: 1571

I didn't manage to find a way to read the text, but I found another solution. Instead of checking what text is displayed on the screen, I check to see which of the labels is being displayed (I'm able to do this because each of the labels is given a unique ID.

So the code will look something like:

Name=driver.find_element_by_id('test2')
if Name.is_displayed():
  print ("Element found")
else:
  print ("Element not found")

Upvotes: 0

Related Questions