1

I'm struggling to get the actual text of the text box as I need it as a text to store it in a variable rather than comparing it against a value, because I need to add it to the end of the url to call another page.

I tried using the code suggested by ebeal but it didn't do what I want:

var access_token = driver.findElement(webdriver.By.name("AccToken"))
                         .getAttribute("value")
                         .then(console.log);

// This outputs the right result but only to the console as I can't save it to a variable

var access_token = driver.findElement(webdriver.By.name("AccToken")) 
                         .getText();

access_token = access_token.then(function(value){
                                   console.log(value);
                                });

console.log("the new one : " + access_token); 
// this one outputs :  the new one:     Promise::304 {[[PromiseStatus]]: "pending"}

Any idea?

4 Answers 4

3

WebdriverJS is purely asynchronous. Meaning, you need to provide a callback and instantiate your variable inside the callback rather than simply assigning the call the results of the function to your variable.

That's why you will always get a promise everytime you console.log your access_token variable. The webdriverjs docs explain a little about how promises work in selenium-webdriver https://code.google.com/p/selenium/wiki/WebDriverJs#Understanding_the_API

You can do the following to assign the text to a variable:

var access_token;    

var promise = driver.findElement(webdriver.By.name("AccToken")).getText();

promise.then(function(text) {
    access_token = text;
});

I highly recommend WebdriverIO as it takes away from the pain of having to write your own promises. http://webdriver.io/

Sign up to request clarification or add additional context in comments.

Comments

3

So this is one thing that I have had to learn the hard way, so I hope this helps:

var access_token = await driver.findElement(webdriver.By.name("AccToken"))
    .getAttribute("value")
    .then((value) => { return value; });

Comments

1

I'm not sure which version of Webdriver you are using, but you may have some luck using WebdriverIO. Specifically its getText() function which will return a callback with the text so you can use it elsewhere.

http://webdriver.io/api/property/getText.html

client.getText('#elem').then(function(text) {
    console.log(text);
});

Comments

0

This should work just fine if you are looking to just get the value. If you are using the new await ES6 syntax no need to "then" the promise.

const { Builder, By, Key, until } = require('selenium-webdriver');
const assert = require('assert');
    
let access_token = await driver.findElement(By.name("AccToken")).getAttribute("value");

Then you can even just assert:

assert.equal(access_token, "your token value here");

More information can be found at the documentation for selenium-webdriver. Take a look at the Webdriver instance methods for a closer look. Good luck!

Comments

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.