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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With