What is the difference between driver.switchTo().parentFrame() and driver.switchTo().defaultContent() method in Selenium WebDriver?

大兔子大兔子 提交于 2020-06-10 05:27:09

问题


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 .



来源:https://stackoverflow.com/questions/51096800/what-is-the-difference-between-driver-switchto-parentframe-and-driver-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!