简体   繁体   English

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

[英]How to get value of textbox in selenium webdriver , Js

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.我正在努力获取文本框的实际文本,因为我需要将其作为文本存储在变量中,而不是将其与值进行比较,因为我需要将其添加到 url 的末尾以调用另一个页面.

I tried using the code suggested by ebeal but it didn't do what I want:我尝试使用 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"}

Any idea?任何的想法?

WebdriverJS is purely asynchronous. WebdriverJS 是纯异步的。 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.这就是为什么每次你 console.log 你的 access_token 变量时你都会得到一个承诺。 The webdriverjs docs explain a little about how promises work in selenium-webdriver https://code.google.com/p/selenium/wiki/WebDriverJs#Understanding_the_API webdriverjs 文档解释了一些关于承诺在 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.我强烈推荐 WebdriverIO,因为它消除了必须编写自己的 Promise 的痛苦。 http://webdriver.io/ 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.我不确定您使用的是哪个版本的 Webdriver,但使用 WebdriverIO 可能会有些运气。 Specifically its getText() function which will return a callback with the text so you can use it elsewhere.特别是它的 getText() 函数将返回带有文本的回调,以便您可以在其他地方使用它。

http://webdriver.io/api/property/getText.html 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.如果您使用新的 await ES6 语法,则无需“那么”承诺。

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 .更多信息可以在selenium-webdriver的文档中找到。 Take a look at the Webdriver instance methods for a closer look.看一看 Webdriver 实例方法以进一步了解。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 node.js 在 selenium webdriver 中选择下拉值 - how to select a dropdown value in selenium webdriver using node.js 使用Selenium WebDriver获取JavaScript提示框的值 - Get the value of a JavaScript prompt box with Selenium WebDriver 在Selenium Webdriver ruby​​脚本中获取元素值 - Get element value in Selenium webdriver ruby script selenium js webdriver.By - selenium js webdriver.By 如何存储随机值并通过Jasmine JS的Selenium Webdriver中的click函数调用它? - How to store a random value and call it from a click function in selenium webdriver by Jasmine JS? Selenium Webdriver-仅具有精确值标识的元素,js / html - Selenium Webdriver - elements only with exact value identification , js/html 如何使用Knockout.js获取文本框onchange的值 - How to get the value of a textbox onchange with knockout.js 在php webdriver client和selenium上运行时如何获取HTML span标签的值 - How to get value of HTML span tag while running on php webdriver client and selenium 如何使用Selenium WebDriver,NUnit和C#获取元素属性的子属性值 - How to get child property value of a element property using selenium webdriver, NUnit and C# Selenium Webdriver Java:如何从禁用的下拉列表中获取显示的值 - Selenium Webdriver Java: How to get the displayed value from a disabled dropdown list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM