如何使用 selenium 和 Mocha 获取 xPath() 选择的锚标记的文本
How to get the text of an anchor tag selected by xPath() using selenium and Mocha
我已经成功选择了一个 <a>
标签。我想显示锚标签的文字,但无法显示。
我正在使用 selenium、mocha、javascript 和 phantomJS
这是我的脚本(完整详细):
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")>-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.");
}
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 > 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 > 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 > 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();
});
});
1。情况 1:使用 e[0].text
我的问题出在这个脚本中。
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
}
});
如您所见,console.log("Username : "+e[0].text);
导致了问题。
为方便起见,这是我收到的完整消息。
C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>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)
2。情况 2:使用 e.text
现在,当我进行如下更改时:
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.text);//this is the line with the issue. It prints undefined
}
});
这是我收到的消息。
C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>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)
3。案例 3:使用 e[0].getText()
我做了以下更改:
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].getText());
}
});
消息如下:
C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>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)
这是 HTML:
<ul class="nav navbar-top-links navbar-right">
<li>
<a class="user-name m-r-sm text-muted welcome-message" href="/profile/">saswat@matrixnmedia.com</a>
</li>
<li>
<a href="http://saswatr3.ouh.co/main/account/help.php">
<i class="fa fa-life-ring"></i>
</a>
</li>
<li>
<a class="log-out" href="/logout">
<i class="fa fa-sign-out"></i>
Log out
</a>
</li>
</ul>
您正在使用复数 findElements,它会为您提供一个元素列表。对列表使用 e.text 将不起作用,因为您只能将 .text 与 webobject 一起使用。
使用单数形式获取页面上的第一个匹配项:driver.findElement()
如果您只需要获取列表中一个元素的文本,请使用 e[0].text 获取列表中第一个元素的文本。
你可以试试这个方法:
driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getText().then(function(text){
console.log("Username : " + text);
});
- 您只需要通过 findElement 搜索(而不是 findElements)
- 直接把函数部分前面的文字提取出来
更新:
有时对象并没有真正隐藏,也不在视口中,那么gettext()也是returns一个空字符串。
要检查,请尝试以下操作:
driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getAttribute("innerText").then(function(text){
console.log("Username : " + text);
});
我已经成功选择了一个 <a>
标签。我想显示锚标签的文字,但无法显示。
我正在使用 selenium、mocha、javascript 和 phantomJS
这是我的脚本(完整详细):
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")>-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.");
}
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 > 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 > 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 > 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();
});
});
1。情况 1:使用 e[0].text
我的问题出在这个脚本中。
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
}
});
如您所见,console.log("Username : "+e[0].text);
导致了问题。
为方便起见,这是我收到的完整消息。
C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>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)
2。情况 2:使用 e.text
现在,当我进行如下更改时:
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.text);//this is the line with the issue. It prints undefined
}
});
这是我收到的消息。
C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>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)
3。案例 3:使用 e[0].getText()
我做了以下更改:
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].getText());
}
});
消息如下:
C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>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)
这是 HTML:
<ul class="nav navbar-top-links navbar-right">
<li>
<a class="user-name m-r-sm text-muted welcome-message" href="/profile/">saswat@matrixnmedia.com</a>
</li>
<li>
<a href="http://saswatr3.ouh.co/main/account/help.php">
<i class="fa fa-life-ring"></i>
</a>
</li>
<li>
<a class="log-out" href="/logout">
<i class="fa fa-sign-out"></i>
Log out
</a>
</li>
</ul>
您正在使用复数 findElements,它会为您提供一个元素列表。对列表使用 e.text 将不起作用,因为您只能将 .text 与 webobject 一起使用。
使用单数形式获取页面上的第一个匹配项:driver.findElement()
如果您只需要获取列表中一个元素的文本,请使用 e[0].text 获取列表中第一个元素的文本。
你可以试试这个方法:
driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getText().then(function(text){
console.log("Username : " + text);
});
- 您只需要通过 findElement 搜索(而不是 findElements)
- 直接把函数部分前面的文字提取出来
更新:
有时对象并没有真正隐藏,也不在视口中,那么gettext()也是returns一个空字符串。
要检查,请尝试以下操作:
driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getAttribute("innerText").then(function(text){
console.log("Username : " + text);
});