Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting nested iframe - selenium / javascript / node-js

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.

like image 742
F. Rakes Avatar asked Dec 05 '13 13:12

F. Rakes


People also ask

How do I switch from one iframe to another in Selenium?

Another way to switch between frames in selenium is to pass the WebElement to the switchTo() command. Here the primary step is to find the iframe element and then pass it to the switch method.

How do I click iframe in Selenium?

Right click on the element, If you find the option like 'This Frame' then it is an iframe. (Please refer the above diagram) Right click on the page and click 'View Page Source' and Search with the 'iframe', if you can find any tag name with the 'iframe' then it is meaning to say the page consisting an iframe.

How do I switch to frame in Javascript?

Using switchTo().defaultContent() The switchTo(). defaultContent() method will switch focus to the first frame on the page. In the case of iFrames, the context is switched back to the main (or Parent) document. You can also refer to the below video tutorial on how to handle Windows and Frames in selenium.


1 Answers

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.

like image 154
djangofan Avatar answered Oct 04 '22 10:10

djangofan