I have a nested iframe. I am in child frame currently,Now i want to switch to main iframe and then from mainframe i want to switch to normal frames.How to do this?
-
3what do you mean by normal frames? – Murthi Jul 18 '18 at 07:06
-
1Possible duplicate of [How to switch between frames in Selenium WebDriver using Java](https://stackoverflow.com/questions/10879206/how-to-switch-between-frames-in-selenium-webdriver-using-java) – Ashok kumar Ganesan Jul 18 '18 at 07:07
3 Answers
If you are in the child frame
, switch to default content like this:
driver.switchTo().defaultContent();
Now you are on the main HTML and you can switch to every frame again. If you want to switch on nested frames, you have to do it step by step.
More information you can get in this tutorial.

- 5,559
- 5
- 22
- 48
Scenario : Nested iframe.
iframeMain
iframeParent
iframechild
Assume you are in ifrmaechild :
When you do driver.switchTo().parentFrame();
: you will go to iframeParent .
But when you do driver.switchTo().defaultContent();
: you will go to main HTML of page.
Note that in this case you will not go to iframeMain.
So, in your case you should do : driver.switchTo().parentFrame();
to go to iframe parent. and from here you should use : driver.switchTo().defaultContent();
to go to default DOM tree.
Hope this will help.

- 28,520
- 6
- 20
- 38
-
-
@vigneshvicks :If this answer was helpful , please accept it by clicking on check mark which is just besides my answer and under the down vote button. Thank you ! – cruisepandey Jul 19 '18 at 12:43
As per your scenario, Currently you are in childFrame
, Now you want to go back to mainFrame
from childFrame
like childFram > mainFrame > childFrame
.
Basically, we can switch over the elements in frames using 3 ways
- By Index
- By Name or Id
- By Web Element
You can switch frame by index or cssSelector. I have also having same problem like you. I implemented resolution for my problem like below.
//Below will gives you number of frames available in current page.
Int size = driver.findElements(By.tagName("iframe")).size();
//Switch to frame by it's index
driver.switchTo().frame(4);
//To write text in iframe using below code.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
body.sendKeys("Some text");
//below code will switch to default DOM
driver.switchTo().defaultContent();
Hope, This will work for you.

- 435
- 5
- 18