Franco Manzur
Franco Manzur

Reputation: 373

Nightwatch - Get an element which class is duplicated in the DOM

I'm running a functional test with NightwatchJs and i got a html similar to this one.

<div>
  <span class="ps-cls">value 1</span>
</div>
 <div>
  <span class="ps-cls">value 2</span>
</div>

What i need is to get the value of the second span (with value 2). I tried with pseudoclass but i couldn't get it.

How can i get that kind of element? (a nodeChild with duplicated class).

Upvotes: 0

Views: 2884

Answers (3)

Harjinder Banga
Harjinder Banga

Reputation: 13

    .elements('css selector', '#row table-row ng-scope', function (result) {
        console.log(result + 1);
        for (var i=0; i<result.value.length; i++) {
            result.value.length - 1;
        this.elementIdAttribute(result.value[i].ELEMENT, 'Add Condition', function 
(result) {
        console.log(result + 2);
        this.click(result.value)
        })
        }
        })

Upvotes: 0

Ray
Ray

Reputation: 1676

Using xpath : //div[2]/span[@class="ps-cls"]

You can use .getText() to get the value of span

var a_text = '';
browser.getText('//div[2]/span[@class="ps-cls"]',function(object){
  a_text=object.value;
})

.getText() is returning a WebElement JSON OBJECT rather than a string, for more detailed explanation, you can refer to my answer in another question:

https://stackoverflow.com/a/45639217/8444504

Upvotes: 1

QualiT
QualiT

Reputation: 1955

Try this:

 browser.getText("div:nth-of-type(2) > .ps-cls", function(result) {
    this.assert.equal(typeof result, "object");
    this.assert.equal(result.status, 0);
    this.assert.equal(result.value, "nightwatchjs.org");
  });

nightwatch.js getText

Upvotes: 0

Related Questions