admin管理员组

文章数量:1302243

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.

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 edited Dec 6, 2013 at 16:19 F. Rakes asked Dec 5, 2013 at 13:33 F. RakesF. Rakes 1,5373 gold badges17 silver badges25 bronze badges 3
  • Before switching to the secondframe try to switch back to default content. – Furious Duck Commented Dec 5, 2013 at 15:24
  • Getting a NoSuchFrameError as well. – F. Rakes Commented Dec 5, 2013 at 15:32
  • you can use driver.switchTo().frame("secondframe"); – rüff0 Commented Sep 8, 2023 at 4:25
Add a ment  | 

2 Answers 2

Reset to default 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.

You can use

driver.switchTo().frame(0); 

instead of

driver.switchTo().frame("secondframe");

, as it is the sole child iframe within the parent.

本文标签: nodejsSelecting nested iframeseleniumjavascriptnodejsStack Overflow