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

p4rjhz4m  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(138)

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

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();
  });
});

使用下面的代码,我尝试检查是否存在文本为“Welcome to the admin page!”的<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>
wqsoz72f

wqsoz72f1#

请尝试此xpath:
“//*[text()[包含(.,'欢迎使用管理页面!')]]”
伊沃

9avjhtql

9avjhtql2#

“还有一个选择:

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

相关问题