webdriverio 将 getText 字符串设置为变量

webdriverio Set getText string to variable

我目前正在尝试使用 webdriverio 使用 getText 方法的内容实例化一个变量。

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

当我尝试打印变量时,这是输出:

[object Object]

感谢您的帮助!

请使用如下代码,

字符串文本值=driver.findElement(By.cssSelector("")).getText();

By.cssSelector("")是查找元素,可以根据你页面定义的元素使用id或者name或者css

browser.getText() 是一个异步调用,因此您需要提供一个回调来实例化您的变量。试试这个:

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

可以在 Webdriverio 开发人员指南中找到类似的示例:http://webdriver.io/guide.html

此外,由于此方法 returns 一个字符串,因此无需将变量转换为字符串。参见 https://github.com/webdriverio/webdriverio/blob/master/lib/commands/getText.js