2015-07-01 43 views
1

我很努力地获取文本框的实际文本,因为我需要它作为文本存储在变量中,而不是将其与值进行比较,因为我需要将其添加到URL的末尾以调用另一个页面。如何获得selenium webdriver中的文本框的值,Js

我试图使用ebeal建议的代码,但它并没有做什么,我想:

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

任何想法?

回答

1

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

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

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

WebdriverJS纯粹是异步的。这意味着,您需要提供回调并在回调中实例化您的变量,而不是将函数的结果简单地分配给您的变量。

这就是为什么你每次使用console.log你的access_token变量都会得到承诺的原因。该webdriverjs文档解释一点有关承诺,在硒的webdriver如何工作https://code.google.com/p/selenium/wiki/WebDriverJs#Understanding_the_API

你可以做以下的文本分配给一个变量:

var access_token;  

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

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

我强烈建议WebdriverIO,因为它从痛苦带走不得不写你自己的承诺。 http://webdriver.io/

相关问题