如何使用 WebdriverIO 获取 Window 变量
How to Get Window Variable Using WebdriverIO
我正在尝试 运行 带有 PhantomJS/Chrome 的 webdriverio 加载页面,然后抓取 window 对象以与其他脚本一起使用。由于某种原因,我无法获得 window 对象。每次我得到,我最终都会看到这样的输出:
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();
有谁知道如何修复我的代码,以便 window 对象在页面完全加载后返回到我的 NodeJS 控制台应用程序?
谢谢!
Window 是浏览器 DOM 中的对象,因此它仅在 'execute' 函数内部可用。如果你想访问它,你可以从你的 'execute' 函数 return 它:
return client.execute(function () {
return window;
}).then(function (result) {
console.log(result);
});
这也有效:
browser.execute('return window');
我正在尝试 运行 带有 PhantomJS/Chrome 的 webdriverio 加载页面,然后抓取 window 对象以与其他脚本一起使用。由于某种原因,我无法获得 window 对象。每次我得到,我最终都会看到这样的输出:
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();
有谁知道如何修复我的代码,以便 window 对象在页面完全加载后返回到我的 NodeJS 控制台应用程序?
谢谢!
Window 是浏览器 DOM 中的对象,因此它仅在 'execute' 函数内部可用。如果你想访问它,你可以从你的 'execute' 函数 return 它:
return client.execute(function () {
return window;
}).then(function (result) {
console.log(result);
});
这也有效:
browser.execute('return window');