admin管理员组文章数量:1430571
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 paring 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?
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 paring 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?
Share Improve this question edited Jul 1, 2015 at 16:33 nickbdyer 6441 gold badge6 silver badges13 bronze badges asked Jul 1, 2015 at 11:12 user3260891user3260891 331 silver badge3 bronze badges4 Answers
Reset to default 3WebdriverJS 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./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 remend WebdriverIO as it takes away from the pain of having to write your own promises. http://webdriver.io/
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; });
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);
});
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!
本文标签: javascriptHow to get value of textbox in selenium webdriverjsStack Overflow
版权声明:本文标题:javascript - How to get value of textbox in selenium webdriver , Js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744348261a2601894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论