选择嵌套的iframe - selenium/javascript/node-js

F. *_*kes 8 javascript iframe selenium node.js

我想在node-js中使用selenium webdriver模块在iframe中选择嵌套的iframe.

例如:

<iframe id="firstframe">
   <div id="firstdiv"></div>
   <iframe id="secondframe">
     <div id="seconddiv"></div>
   </iframe>
</iframe>
Run Code Online (Sandbox Code Playgroud)

对于node-js部分:

driver.switchTo().defaultContent();
driver.switchTo().frame("firstframe");   // --> works
driver.switchTo().frame("secondframe");  // --> NoSuchFrameError


iframes = driver.findElements(webdriver.By.tagName('iframe')).then(function(elements){
            console.log(elements.length); // --> if I put this code before the switch to first frame output: 1, if I put it after output: 0)
            });
Run Code Online (Sandbox Code Playgroud)

我尝试使用索引号,但这也失败了.

编辑:

好吧,我想通了,但我的答案被用户@casparOne因某些原因删除了.如果有人仍然想知道这里的问题是什么:

我上面的代码工作,只是不在本地.Chrome的安全设置拒绝在本地文件的iframe中更深入.因此它甚至没有显示iframe的源代码.

dja*_*fan 5

我以前做过几次这样的事情。尝试在切换帧之间暂停 1 秒。(有时)在尝试另一个切换之前,您需要给 Selenium(或浏览器)足够的时间来加载框架 DOM。