4

What is difference between below two methods :

driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sreenath reddy
  • 103
  • 1
  • 2
  • 9

4 Answers4

5

driver.switchTo().parentFrame();

As per the specifications, driver.switchTo().parentFrame(); invokes the following:

Switch To Parent Frame

Where, the Switch to Parent Frame command sets the current browsing context for future commands to the parent of the current browsing context.

As per the Java Docs parentFrame() method changes the focus to the parent context. If the current context is the top level browsing context, the context remains unchanged.

driver.switchTo().defaultContent();

As per the Java Docs, defaultContent() method selects either the first frame on the page, or the main document when a page contains iframes.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • That mean for example If i have 3 frames which are nested frames i1, i2, i3. If my cursor currently in i3. By using parentframe() i can reach to i2 and i can reach to i1 by using defaultcontent() – sreenath reddy Jun 30 '18 at 11:23
  • @sreenathreddy Correct `If my cursor currently in i3. By using parentframe() i can reach to i2` but `defaultcontent()` will take you to the **main document** i.e. [**Top-level browsing context**](https://w3c.github.io/webdriver/#dfn-top-level-browsing-contexts) – undetected Selenium Jun 30 '18 at 11:42
2

driver.switchTo().defaultContent();

This will pass the control to the main document which contains the iframes

driver.switchTo().parentFrame();

This will pass the control to the imminent parent frame of the current frame

Lets understand it:

main body
{
frame1
{
frame2
frame3 (we are here currently)
}
}

Now using driver.switchTo().defaultContent(); will pass the control to main body

And using driver.switchTo().parentFrame(); will pass the control to frame1 .

1

When you are dealing with multiple iframes in your webpage, then driver.switchTo().parentFrame() is generally used to switch the control back to the parent frame.

When you deal with pop-up dialog windows within your webpage, then driver.switchTo().defaultContent() is used to switch the control back to default content in the window.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
JaySin
  • 13
  • 3
0

There is clearly a difference :

Scenario : When there are multiple frames and some of them are nested.

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 .

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • driver.switchTo().defaultContent(); in this case focus will move to parent frame of all nested frames. for Example, I have i1,i2,i3 frames are there which are nested frames. in this case, if I use default content() when focus at i3, it moves to the i1 frame. – sreenath reddy Jul 09 '18 at 06:44
  • if you use `driver.switchTo().defaultContent();` from i3 , you will be moved to main HTML DOM tree. – cruisepandey Jul 10 '18 at 17:02