0

当我调用 switchToWindow(handle): null value in entry: name=null 时出现以下错误

当我尝试切换并且句柄不为空或为空时,原始窗口仍然打开。这是我正在使用的代码:

var session = this.remote;
var handle;

return session
    .get('http://www.google.com')
    .getCurrentWindowHandle()
    .then(function (currentHandle) {
        console.log('handle: ' + currentHandle);
        handle = currentHandle;
    })
    .execute(function() {
        var newWindow = window.open('https://www.instagram.com/', 'insta');
    })
    .switchToWindow('insta')
    .closeCurrentWindow()
    .then(function () {
        console.log('old handle: ' + handle);
    })
    .sleep(2000)
    .switchToWindow(handle);
4

1 回答 1

1

命令链是一个单一的 JavaScript 表达式。这意味着链中所有调用的所有参数都会被同时评估一次。当在靠近链顶部的回调handle中分配时then,它不会影响switchToWindow链底部的调用,因为在回调执行handle之前已经评估了 的值。then

如果您想在链的早期保留对值的引用,并在以后使用它,那么这两种用法都应该在then回调中。

return session
    ...
    .then(function (currentHandle) {
        handle = currentHandle;
    })
    ...
    .then(function () {
        return session.switchToWindow(handle);
    });
于 2016-04-28T01:31:22.623 回答