vignesh vicks
vignesh vicks

Reputation: 11

How to switch from child iframe to main iframe and again from main iframe to normal frames?

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?

Upvotes: 1

Views: 907

Answers (3)

BhavinD.
BhavinD.

Reputation: 481

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.

Upvotes: 1

Andrei
Andrei

Reputation: 5637

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.

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

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.

Upvotes: 1

Related Questions