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);
                    })
                });
  • @TESTasy please check the updated question. – azhar Mar 25 '17 at 10:52
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);
});
  • I just want selected option's text ? Is it possible? – azhar Mar 25 '17 at 18:17
  • 1
    @azhar - in Niels example, change getAttribute('value') to getAttribute('text') – Swagin9 Apr 24 '17 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.