无法使用Selenium中的xpath()获取带有特定文本的<p>标记

时间:2015-08-18 13:13:43

标签: selenium xpath mocha

我有一个脚本,我试图在使用Mocha的Selenium环境中运行:

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;

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('saswat@matrixnmedia.com');
        driver.findElement(By.id('password')).sendKeys('DarkPrince2012');
        driver.findElement(By.id('_submit')).click();
        driver.wait(until.titleIs('Track Revenue'), 1000);
        if(driver.findElements(By.xpath("//p[text()[contains(.,'Welcome to the admin page!')]]")).size() != 0)
        {

            loginFlag = 1;
        }

        if(loginFlag ==1)
                console.log("Login Successful");
            else
                console.log("Login Unsuccessful");
    driver.quit();
  });
});

使用以下代码,我正在尝试检查是否存在<p>标记,其中包含“欢迎使用管理员页面

的文本

if(driver.findElements(By.xpath("//p[text()[contains(.,'Welcome to the admin page!')]]")).size() != 0)

但是我收到以下错误:

C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>mocha -t 50000 testMocha\login-as-administrator-mocha.js


  TrackRevenue Test
Verification Successful - The correct title is displayed on the web page.
    1) should work


  0 passing (21s)
  1 failing

  1) TrackRevenue Test should work:
     TypeError: undefined is not a function
  From: Task: TrackRevenue Test should work

我做错了什么?

以下是我要查找的<p>的HTML:

<div class="wrapper wrapper-content animated fadeIn">
   <div class="row">
     <div class="col-centered col-xs-12">
     <div class="ibox float-e-margins">
     <div class="ibox-title">
       <h5>Links</h5>
       <div class="ibox-tools"> </div>
     </div>
     <div class="ibox-content">
        <p>Welcome to the admin page!</p>
        <p>Please use the menu on the left</p>
     </div>
     </div>
     </div>
   </div>
</div>

2 个答案:

答案 0 :(得分:2)

请试试这个xpath:

&#34; // * [text()[包含(。,&#39;欢迎使用管理员页面!&#39;)]]&#34;

伊沃。

答案 1 :(得分:0)

“还有一个选择:

IList<IWebElement> element = driver.FindElements(By.xpath("//div[@class='ibox-content']/p")

foreach(IWebElement e in element)
if(e.Text.contains("Welcome to the admin page!"))
// do stuff