webdriverio Set getText string to variable

2019-07-08 11:07发布

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!

2条回答
走好不送
2楼-- · 2019-07-08 11:22

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

查看更多
欢心
3楼-- · 2019-07-08 11:28

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

查看更多
登录 后发表回答