Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Before switching to the secondframe try to switch back to default content. –  Alexander Petrovich Dec 5 '13 at 15:24
    
Getting a NoSuchFrameError as well. –  F. Rakes Dec 5 '13 at 15:32

1 Answer 1

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.

share|improve this answer
    
driver.switchTo().frame("firstframe"); setTimeout(function(){driver.switchT‌​o().frame("secondframe");}, 5000); still getting NoSuchFrameError. I also tried using a .then()-promise after the first. –  F. Rakes Dec 5 '13 at 15:50
    
You can troubleshoot by using XPath to get all elements from the dom matching tag name "iframe". –  djangofan Dec 5 '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 (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 at 18:06
    
I think that "Control Flow" think you refer to is an older Selenium 1.0 thing? –  djangofan May 12 at 23:17
    
I have done similar, it is so weird to use timeouts for this. –  Shane May 15 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.