最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to set value into textarea attribute using nightwatch.js - Stack Overflow

matteradmin7PV0评论

I am working on nighwatch.js for web ui testing, I want to set value to a textarea, and textarea has an attribute which has my actual text, I am writing full textarea and how I am setting in following.

<div class="textarea-description">
    <textarea cols="50" class="myClass setText" data-def placeholder="text to be replaced using nightwatch"/>
</div>

I am trying to set value in above textarea's attribute data-def placeholder as following ways

browser.setValue('.textarea-description textarea[type=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[data-def placeholder=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[type=data-def placeholder]','nightwatch');

but nothing is working.

I am working on nighwatch.js for web ui testing, I want to set value to a textarea, and textarea has an attribute which has my actual text, I am writing full textarea and how I am setting in following.

<div class="textarea-description">
    <textarea cols="50" class="myClass setText" data-def placeholder="text to be replaced using nightwatch"/>
</div>

I am trying to set value in above textarea's attribute data-def placeholder as following ways

browser.setValue('.textarea-description textarea[type=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[data-def placeholder=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[type=data-def placeholder]','nightwatch');

but nothing is working.

Share Improve this question edited Jan 29, 2016 at 11:55 ROMANIA_engineer 56.8k30 gold badges210 silver badges205 bronze badges asked Jan 29, 2016 at 11:54 Ashish-BeJovialAshish-BeJovial 1,8675 gold badges39 silver badges67 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

This might not be the best solution but it works:

browser
.execute("document.querySelector('.textarea-description .myClass').setAttribute('placeholder', 'nightwatch');")

If you have jQuery you can make it a bit nicer:

browser
.execute("$('.textarea-description .myClass').attr('placeholder', 'nightwatch');")

Thank you for your all valuable suggestions, all suggestions provided by you was able to give good knowledge but unfortunately none of the suggestion worked. I have resolved it by using following.

client.setValue('.textarea-description textarea','new text to be write.');

Actually attribute "data-def placeholder" was using only watermark that was not actual text, so it is working.

You could use xpath to get the attribute.

.useXpath().setValue('//textarea[contains(@placeholder,'text to be replaced using nightwatch')]@placeholder', 'nightwatch')

How to select specified node within Xpath node sets by index with Selenium?

This worked for me.

.assert.visible('div.textarea-description textarea.setText')
.moveToElement('div.textarea-description textarea.setText', null, null)
.mouseButtonClick('left')
.keys([browser.Keys.CONTROL, "a"])
.keys([browser.Keys.CONTROL, "nightwatch"])
Post a comment

comment list (0)

  1. No comments so far