title: How to get the text of an anchor tag selected by xPath() using selenium and Mocha
body: <p>I have successfully selected an <code>&lt;a&gt;</code> tag. I want to display the text of the anchor tag and I am unable to do so.</p>
  
  <p>I am using selenium, mocha, javascript and phantomJS</p>
  
  <p>Here's my script(full in detail):</p>
  
  <pre><code>var assert = require('assert');
  var test = require('selenium-webdriver/testing');
  var webdriver = require('selenium-webdriver');
  var By = webdriver.By;
  var until = webdriver.until;
  var equals = webdriver.equals;
  
  /*-------login details--------*/
  var userAdmin = 'saswat@matrixnmedia.com';
  var passAdmin = 'DarkPrince2012';
  
  var userTradeshow = 'joni@mailinator.com';
  var passTradeshow = 'Mithun@';
  /*-----login details ends-----*/
  
  /*---setting up credentials---*/
  var passArgument = process.env.KEY; /*fetch value from the environment value;*/
  console.log("You chose to enter as '"+passArgument+"'");
  if(passArgument.toLowerCase().indexOf("admin")&gt;-1)
  {
      var username = userAdmin,
          password = passAdmin;
  }
  else if(passArgument.toLowerCase().indexOf("trade")&gt;-1)
  {
      var username = userTradeshow,
          password = passTradeshow;
  }   
  else
  {
      var username = "",
          password = "";
  }    
  /*-setting up credentials ends-*/
  
  test.describe('TrackRevenue Test', function() 
  {
    test.it('should work', function() 
    {
          var driver = new webdriver.Builder()
                      .withCapabilities(webdriver.Capabilities.phantomjs())
                      .build();
          var loginFlag = 0;
          var baseUrl = 'http://saswatr3.ouh.co/login';
          var expectedTitle = "Track Revenue";
          var successMessage = "Welcome to the admin page!";
          driver.get(baseUrl);
          driver.getTitle().then(function(title) 
          {
              if(expectedTitle === title)
              {
                  console.log("Verification Successful - The correct title is displayed on the web page.");
              }
              else
              {
                  console.log("Verification Failed - An incorrect title is displayed on the web page.");
              }
          });
          driver.findElement(By.id('username')).sendKeys(username);
          driver.findElement(By.id('password')).sendKeys(password);
          driver.findElement(By.id('_submit')).click();
          driver.findElements(By.xpath("//a[contains(text(), 'Log out')]")).then(function(elements_arr)
          {
              if(elements_arr.length &gt; 0)
              {
                  loginFlag = 1;
                  driver.findElements(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).then(function(e){
                      if(e.length &gt; 0)
                      {
                          console.log("No. of elements :"+e.length);
                          console.log("Found The USerName : ");
                          console.log("Username : "+e[0].text);//this is the line with the issue. It prints undefined
                      }
                  });
              }
              else
              {
                  driver.findElements(By.xpath("//div[contains(text(), 'Invalid credentials.')]")).then(function(elements_arr2)
                  {
                     if(elements_arr2.length &gt; 0)
                          console.log("Login Unsuccessful, div invalid credentials found");
                     else
                          console.log("Login Unsuccessful, div invalid credentials not found");
                  });
              } 
              if(loginFlag == 1)
                  console.log("Login Successful");
              else
                  console.log("Login Unsuccessful");
          });
      driver.quit();
    });
  });
  </code></pre>
  
  <h3>1. Case 1: With e[0].text</h3>
  
  <p>My problem lies within this script.</p>
  
  <pre><code>driver.findElements(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).then(function(e){
                          if(e.length &gt; 0)
                          {
                              console.log("No. of elements :"+e.length);
                              console.log("Found The USerName : ");
                              console.log("Username : "+e[0].text);//this is the line with the issue. It prints undefined
                          }
                      });
  </code></pre>
  
  <p>As you can see, <code>console.log("Username : "+e[0].text);</code> is causing problem.</p>
  
  <p>For convenience, this is the full message I am getting.</p>
  
  <pre><code>    C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver&gt;env KEY=trade moch
  a -t 120000 testMocha/login-as-administrator-mocha.js
  You chose to enter as 'trade'
  
  
    TrackRevenue Test
  Verification Successful - The correct title is displayed on the web page.
  Login Successful
  No. of elements :1
  Found The USerName :
  Username : undefined
      √ should work (71593ms)
  
  
    1 passing (1m)
  </code></pre>
  
  <h3>2. Case 2: With e.text</h3>
  
  <p>Now, when I made the changes like:</p>
  
  <pre><code>driver.findElements(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).then(function(e){
                          if(e.length &gt; 0)
                          {
                              console.log("No. of elements :"+e.length);
                              console.log("Found The USerName : ");
                              console.log("Username : "+e.text);//this is the line with the issue. It prints undefined
                          }
                      });
  </code></pre>
  
  <p>This is the message I get.</p>
  
  <pre><code>C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver&gt;env KEY=trade moch
  a -t 120000 testMocha/login-as-administrator-mocha.js
  You chose to enter as 'trade'
  
  
    TrackRevenue Test
  Verification Successful - The correct title is displayed on the web page.
  Login Successful
  No. of elements :1
  Found The USerName :
  Username : undefined
      √ should work (87006ms)
  
  
    1 passing (1m)
  </code></pre>
  
  <h3>3. Case 3: With e[0].getText()</h3>
  
  <p>I made the following changes:</p>
  
  <pre><code>driver.findElements(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).then(function(e){
                      if(e.length &gt; 0)
                      {
                          console.log("No. of elements :"+e.length);
                          console.log("Found The USerName : ");
                          console.log("Username : "+e[0].getText());
                      }
                  });
  </code></pre>
  
  <p>Here's the message:</p>
  
  <pre><code>C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver&gt;env KEY=trade moch
  a -t 120000 testMocha/login-as-administrator-mocha.js
  You chose to enter as 'trade'
  
  
    TrackRevenue Test
  Verification Successful - The correct title is displayed on the web page.
  Login Successful
  No. of elements :1
  Found The USerName :
  Username : Promise::456 {[[PromiseStatus]]: "pending"}
      √ should work (37212ms)
  
  
    1 passing (37s)
  </code></pre>
  
  <p>Here's the HTML:</p>
  
  <pre><code>&lt;ul class="nav navbar-top-links navbar-right"&gt;
     &lt;li&gt;
        &lt;a class="user-name m-r-sm text-muted welcome-message" href="/profile/"&gt;saswat@matrixnmedia.com&lt;/a&gt;
     &lt;/li&gt;
     &lt;li&gt;
        &lt;a href="http://saswatr3.ouh.co/main/account/help.php"&gt;
        &lt;i class="fa fa-life-ring"&gt;&lt;/i&gt;
     &lt;/a&gt;
     &lt;/li&gt;
     &lt;li&gt;
        &lt;a class="log-out" href="/logout"&gt;
        &lt;i class="fa fa-sign-out"&gt;&lt;/i&gt;
         Log out
        &lt;/a&gt;
     &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>