0

I am trying to navigate to iframe and come back to top frame using selenium.

<html>
    <body>
        <div id="testID">This is upper frame</div>
        <iframe src="test.html"></iframe>
     </body>
</html>

I want to get the testID which I am getting easily using driver.findElement(By.id("testID")).

And then I want to navigate to iframe using driver.switchTo().frame(driver.findElement(By.id("new_page"))); where new_page is the id inside test.htm.

Now, my requirement is to switch back so that I can again read testID from the above body.

But if I use: driver.switchTo().frame(driver.findElement(By.id("testID"))); I am getting exception like:

unable to locate element

kenorb
  • 155,785
  • 88
  • 678
  • 743
graphics123
  • 1,191
  • 3
  • 20
  • 57

1 Answers1

1

To switch to frame :

driver.switchTo().frame(0);

To switch to main window back :

driver.switchTo().defaultContent();

Also I suggest to find element of frame first do switch and then find element.

You can switch to frame by following 3 ways :

By Frame Index

By Frame ID or Name

By Frame WebElement

For More Details Please check : Switch to Frame in Selenium

I hope it help you..

Helping Hands
  • 5,292
  • 9
  • 60
  • 127