javascript - Selenium -webdriver npm : how to switch focus to new window from target ="_blank"

标签 javascript selenium selenium-webdriver webdriver

在建立一系列 selenium 测试时,我对 javascript selenium-webdriver npm 有一点障碍。

此特定测试需要 selenium 通过查询加载的页面内容来检查 target="_blank" 链接是否有效。但是我似乎无法让 Selenium 将焦点转移到新窗口。

阅读文档和 S.O.看起来这应该可行:

driver.switchTo().defaultContent();

但这实际上没有任何作用。这是简单的测试:

//1 load up the url
    driver.get( testConfig.url + '/britain/england/cornwall/hotelX' ).then(function(){
        //2 find the link and click it
        driver.findElement(By.css('.details-web')).click().then(function(){
            //3 timeout of 10 seconds to allow the other window to open
            setTimeout(function(){
                //switch focus to the new window (ie the current active window)
                driver.switchTo().defaultContent().then(function(){
                    //5 wait till the new el is visible
                    driver.wait( function(){
                        return driver.isElementPresent(By.css("#infinite-footer"));
                    }, testConfig.timeout).then(function(){
                        driver.close();
                        if( callback ){
                            callback( callback );
                        }
                    });
                });
            },10*1000);
        });
    });

我还发现了这篇文章:Selenium with Webdriver - Switch to child window without name 但它是用 Java 编写的,我尝试将其翻译为 javascript,但没有取得多大进展。

还有其他人设法使用 javascript selenium-webdriver 做到这一点吗?#

谢谢, 约翰

感谢@Stanjer,尝试修复,但现在我在尝试运行 foreach 时得到 undefined is not function...?或“ReferenceError:未定义可用Windows ”

//1 load up the url
        driver.get( testConfig.url + '/britain/england/cornwall/hotelX' ).then(function(){
            //2 find the link and click it
            driver.findElement(By.css('.details-web')).click().then(function(){
                var parent = driver.getWindowHandle();
                driver.sleep(1000);
                var availableWindows = driver.getAllWindowHandles();
                var newWindow = null;
                availableWindows.forEach(function(window) {
                    console.log( window );
                    if (window != parent) {
                        newWindow = window;
                    }
                });
                if (newWindow != null) {
                    driver.switchTo().window(newWindow);

                    driver.wait( function(){
                        return driver.isElementPresent(By.css("#infinite-footer"));
                    }, testConfig.timeout).then(function(){
                        driver.close();
                        if( callback ){
                            callback( callback );
                        }
                    });
                }
            });
        });

availableWidnows 的 console.log:

{ closure_uid_233425945: 273,
  flow_:
   { events_: {},
     closure_uid_233425945: 1,
     activeFrame_:
      { events_: [Object],
        closure_uid_233425945: 29,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: true,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     schedulingFrame_:
      { events_: {},
        closure_uid_233425945: 204,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     shutdownTask_: null,
     eventLoopTask_: null,
     hold_:
      { _idleTimeout: 2147483647,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 410669389,
        _onTimeout: [Function: wrapper],
        _repeat: true },
     yieldCount_: 2 },
  stack_: null,
  parent_:
   { closure_uid_233425945: 271,
     flow_:
      { events_: {},
        closure_uid_233425945: 1,
        activeFrame_: [Object],
        schedulingFrame_: [Object],
        shutdownTask_: null,
        eventLoopTask_: null,
        hold_: [Object],
        yieldCount_: 2 },
     stack_: { [Task: WebDriver.getAllWindowHandles()] name: 'Task' },
     parent_: null,
     callbacks_: [ [Object] ],
     state_: 'pending',
     handled_: true,
     pendingNotifications_: false,
     value_: undefined },
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  pendingNotifications_: false,
  value_: undefined }

最佳答案

所以,如果您想要 js 中该代码的精确副本:

parent = driver.getWindowHandle();

driver.sleep(1000);
var availableWindows = driver.getAllWindowHandles();
var newWindow = null;
availableWindows.foreach(function(window) {
    if (window != parent) {
        newWindow = window;
    }
}
if (newWindow != null) {
    driver.switchTo().window(newWindow);
}

关于javascript - Selenium -webdriver npm : how to switch focus to new window from target ="_blank",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29948320/

相关文章:

javascript - jQuery 文件名

javascript - 在表单输入中指定逗号后的最大长度

python - selenium.common.exceptions.InvalidArgumentException : Message: invalid argument error invoking get() with urls read from text file with Selenium Python

python - 如何在 Firefox geckodriver 中使用代理?

firefox - Selenium WebDriver 默认使用什么配置文件?

javascript - 找出用户是否在前端登录(Laravel)

javascript - 如何使用带样式的组件来设置组件打印的样式?

maven - 使用 Selenium 启动 Firefox

python - 从任务计划程序执行 Python 脚本时出现 0x1 错误

c# - mstest 的 DataSource 属性的 .Net 核心替代方案是什么?