I have successfully chosen an <a>
tag. My goal is to display the text of the anchor tag, but I am facing difficulties.
The technologies being used are selenium, mocha, javascript, and phantomJS
This is the detailed script:
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 = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cffedfffbedf8cce1edf8fee5f4e2e1e9e8e5eda2efe3e1">[email protected]</a>';
var passAdmin = 'DarkPrince2012';
var userTradeshow = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53393c3d3a133e323a3f3a3d32273c217d303c3e">[email protected]</a>';
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")>-1)
{
var username = userAdmin,
password = passAdmin;
}
else if(passArgument.toLowerCase().indexOf("trade")>-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.");
}
...
});
driver.quit();
});
});
1. Case 1: With e[0].text
The issue is with this part of the script.
driver.findElements(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).then(function(e){
if(e.length > 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
}
});
The output shows that
console.log("Username : "+e[0].text);
is causing the problem.
...
Here's the HTML structure:
<ul class="nav navbar-top-links navbar-right">
<li>
<a class="user-name m-r-sm text-muted welcome-message" href="/profile/"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72011301051306321f1306001b0a1c1f17161b135c111d1f">[email protected]</a></a>
</li>
<li>
...