Javascript 切换到父帧webdriver

Javascript 切换到父帧webdriver,javascript,node.js,selenium-webdriver,Javascript,Node.js,Selenium Webdriver,我遇到的问题是使用webdriver.js切换到父帧或iFrame。我尝试实现一个函数来实现这一点,它确实可以工作,但只能达到第二级帧。如果我想从4或5层深的帧切换到父帧,那么这个函数将比现在更有用。其中一个主要问题是,我必须使用for循环来获得帧的顶层,然后循环通过每个帧来获得嵌套帧(如果有)。我必须能够保持循环,直到没有帧有任何帧嵌套。正如您所知,下面的解决方案有大量冗余代码,嵌套到回调地狱的地步。我试图将for循环进程提取到一个helper函数,但我遇到的问题是,我必须切换回顶层,然后沿帧

我遇到的问题是使用webdriver.js切换到父帧或iFrame。我尝试实现一个函数来实现这一点,它确实可以工作,但只能达到第二级帧。如果我想从4或5层深的帧切换到父帧,那么这个函数将比现在更有用。其中一个主要问题是,我必须使用for循环来获得帧的顶层,然后循环通过每个帧来获得嵌套帧(如果有)。我必须能够保持循环,直到没有帧有任何帧嵌套。正如您所知,下面的解决方案有大量冗余代码,嵌套到回调地狱的地步。我试图将for循环进程提取到一个helper函数,但我遇到的问题是,我必须切换回顶层,然后沿帧阶梯向下导航,以检查每个帧是否有嵌套帧。我添加了一些评论来帮助解决这个问题

this.switchToParentFrame = function (frameOrIFrame) {
        //Keep local variable of current frame name
        var currentFrameName;
        //Get current frame name
        this.getCurrentFrameName().then(function (name) {
            currentFrameName = name;
            return driver.switchTo().defaultContent();
        }).then(function () {
            //Find top level frames
            return driver.findElements(webdriver.By.css(frameOrIFrame));
        }).then(function (frames) {
            //Loop through each top level frame to find nested frames
            frames.forEach(function (frame) {
                //Switch back to the default content to check each top level frame
                driver.switchTo().defaultContent().then(function () {
                    return driver.switchTo().frame(frame);
                }).then(function () {
                //Find the nested frames for each top level frame
                    return driver.findElements(webdriver.By.css(frameOrIFrame));
                }).then(function (nestFrames) {
                    //Check to make sure the top level frame has a nested frame
                    if (nestFrames.length > 0) {
                        //Loop through each nested frame to check its name with the current frame name var
                        nestFrames.forEach(function (nestFrame) {
                            //Switch to the default content to switch frames
                            driver.switchTo().defaultContent().then(function () {                                
                                //Switch to the top level frame
                                return driver.switchTo().frame(frame);
                            }).then(function() {
                                //switch to the nested frame
                                return driver.switchTo().frame(nestFrame);
                            }).then(function () {
                                //Get the nested frames name
                                return driver.executeScript('return self.name');
                            }).then(function (name) {
                                //Check to see if the nested frames name is equal to the current frame name var and if so set it equal to the parent frame
                                if (name === currentFrameName){
                                    currentFrameName = frame;
                                }
                            });
                        });
                    }
                });
            });
        }).then(function() {
           return driver.switchTo().defaultContent();
        }).then(function() {
           //Switch to the parent frame
           return driver.switchTo().frame(currentFrameName);
        });
    };
您是否尝试过此方法切换到父帧 您是否尝试过此方法切换到父帧
如果有人提出这个问题

driver.switchTo().parentFrame() 
不适用于NodeJS或Javascript,但

driver.switchTo().defaultContent()

确实有效。

如果有人提出这个问题

driver.switchTo().parentFrame() 
不适用于NodeJS或Javascript,但

driver.switchTo().defaultContent()

确实有效。

此方法在大多数selenium语言中受支持,但在javascript中不受支持。此方法在大多数selenium语言中受支持,但在javascript中不受支持。