如何在selenium webdriver中获取文本框的值,Js

我正在努力获取文本框的实际文本,因为我需要它作为文本存储在一个variables,而不是比较它的价值,因为我需要将其添加到URL的结尾调用另一个页面。

我尝试使用ebealbuild议的代码,但它没有做我想要的:

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"} 

任何想法?

我不确定你正在使用哪个版本的Webdriver,但是使用WebdriverIO可能会有一些运气。 具体来说,它的getText()函数将返回一个callback文本,所以你可以在其他地方使用它。

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

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

WebdriverJS纯粹是asynchronous的。 这意味着,您需要提供一个callback,并在callback中实例化您的variables,而不是简单地将函数的结果分配给您的variables。

这就是为什么你每次使用console.log你的access_tokenvariables都会得到承诺。 webdriverjs文档解释了一些有关承诺如何在selenium-webdriver工作https://code.google.com/p/selenium/wiki/WebDriverJs#Understanding_the_API

您可以执行以下操作将文本分配给variables:

 var access_token; var promise = driver.findElement(webdriver.By.name("AccToken")).getText(); promise.then(function(text) { access_token = text; }); 

我强烈推荐WebdriverIO,因为它不必担心写出自己的承诺。 http://webdriver.io/