9

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.

F. Rakes
  • 1,487
  • 3
  • 17
  • 25

1 Answers1

5

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.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • `driver.switchTo().frame("firstframe");` `setTimeout(function(){driver.switchTo().frame("secondframe");}, 5000);` still getting `NoSuchFrameError`. I also tried using a `.then()`-promise after the first. – F. Rakes Dec 05 '13 at 15:50
  • You can troubleshoot by using XPath to get all elements from the dom matching tag name "iframe". – djangofan Dec 05 '13 at 19:56
  • What about: driver.switchTo().frame("firstframe"); driver.sleep("2000"); driver.switchTo().frame("secondframe"); Webdriver js manages selenium promises by using what is called a "Control Flow", basically it's a promises manager (https://code.google.com/p/selenium/wiki/WebDriverJs#Control_Flows). Try using "official" waiters instead of setTimeout. In this case driver.sleep should do the job. – cSn May 12 '14 at 18:06
  • I think that "Control Flow" think you refer to is an older Selenium 1.0 thing? – djangofan May 12 '14 at 23:17
  • 1
    I have done similar, it is so weird to use timeouts for this. – Shane May 15 '14 at 17:14