如何使用WebdriverIO获取窗口变量

时间:2017-05-20 19:06:15

标签: node.js selenium-webdriver webdriver-io

我正在尝试使用PhantomJS / Chrome运行webdriverio来加载页面,然后抓取窗口对象以便与其他脚本一起使用。由于某种原因,我无法获得窗口对象。每次我得到,我最终看到这样的输出:

Title is: XXXXX
{ state: 'pending' }

使用以下脚本:

var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        browserName: 'chrome',
        logLevel: 'verbose'
    }
};

var client = webdriverio.remote(options);

client
     .init()
     .url('https://xxxx.com')
     .waitUntil(function () {
         return client.execute(function () {
             return Date.now() - window.performance.timing.loadEventEnd > 40000;
        }).then(function (result) {
             console.log(window);
             return window;
         });
     })
     .end();

有没有人知道如何修复我的代码,以便在页面完全加载后将窗口对象返回到我的NodeJS控制台应用程序?

谢谢!

2 个答案:

答案 0 :(得分:4)

Window是浏览器DOM中的一个对象,因此它只能在'execute'函数中使用。如果您想要访问它,可以从 'execute' 函数返回它:

return client.execute(function () {
     return window;
}).then(function (result) {
     console.log(result);
});

答案 1 :(得分:0)

这项工作也是如此:

browser.execute('return window');
相关问题