webdriverio Set getText string to variable

2019-07-08 11:18发布

问题:

I'm currently trying to instantiate a variable with the contents of the getText method using webdriverio.

  a = String(browser.getText('.field_notice'));

When I attempt to print the variable this is the output:

[object Object]

Thanks for the help!

回答1:

browser.getText() is an asynchronous call so you will need to provide a callback to instantiate your variable. Try this :

browser
    .getText('.field_notice').then(function(text) {
        a = text;
    });

A similar example can be found on the Webdriverio Developer Guide : http://webdriver.io/guide.html

Also, there is no need to convert the variable to a string since this method returns a string. See https://github.com/webdriverio/webdriverio/blob/master/lib/commands/getText.js



回答2:

Please use code something like below,

String textValue=driver.findElement(By.cssSelector("")).getText();

By.cssSelector("") is to find elements, you can use id or name or css based on the element defined in your page