Selecting nested iframe - selenium / javascript /

2019-01-26 11:05发布

I want to select a nested iframe within an iframe with the selenium webdriver module in node-js.

For example:

<iframe id="firstframe">
   <div id="firstdiv"></div>
   <iframe id="secondframe">
     <div id="seconddiv"></div>
   </iframe>
</iframe>

for the node-js part:

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)
            });

I tried using the index number but this also failed.

Edit:

Ok, I figured it out but my answer got deleted by user @casparOne for some reason. If anyone still wonders what the problem was here goes:

My code above works, just not locally. Chrome's security settings refuse to go deeper in an iframe on a local file. Hence it didn't even show the source code for the iframe.

1条回答
等我变得足够好
2楼-- · 2019-01-26 11:39

I've done this kind of thing before a few times. Try putting a 1 second pause between the swtich frames. (Sometimes) you need to give Selenium (or the browser) enough time for the frame DOM to load before you try another switch.

查看更多
登录 后发表回答