What is the difference between driver.switchTo().p

2020-06-23 09:55发布

问题:

What is difference between below two methods :

driver.switchTo().parentFrame();
driver.switchTo().defaultContent();

回答1:

driver.switchTo().parentFrame();

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

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.



回答2:

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.



回答3:

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 .



回答4:

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 .