3

How to get the selected option's text using selenium webdriver node.js. In java we can use getFirstSelectedOption but it is not working in node.js. How it is possible?

Here is my sample code.

driver.get("url").then(function () {
                return driver.findElements(webdriver.By.className('test-class'))
            }).then(function (elements) {
                elements.forEach(function(el){
                    el.sendKeys('4');
                })
            }).then(function () {
                return driver.findElements(webdriver.By.className('test-class'))
            }).then(function (elements) {
                elements.forEach(function(el){
                    el.getFirstSelectedOption().getText().then(function(text){
                        assert.equal('4', text);
                    })
                });
1
  • @TESTasy please check the updated question. Commented Mar 25, 2017 at 10:52

1 Answer 1

3

You can use getAttribute() on select elements, should look something like this in your case:

var element = driver.findElement(By.className('test-class')));
element.getAttribute('value').then(function(selected) {
  assert.equal('4', selected);
});
2
  • I just want selected option's text ? Is it possible? Commented Mar 25, 2017 at 18:17
  • 1
    @azhar - in Niels example, change getAttribute('value') to getAttribute('text') Commented Apr 24, 2017 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.