Java Selenium WebDriver 中的 driver.switchTo().parentFrame() 和 driver.switchTo().defaultContent() 方法有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51096800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:57:21  来源:igfitidea点击:

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

javaseleniumselenium-webdriveriframewebdriver

提问by sreenath reddy

What is difference between below two methods :

以下两种方法有什么区别:

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

采纳答案by DebanjanB

driver.switchTo().parentFrame();

driver.switchTo().parentFrame();

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

根据规范,driver.switchTo().parentFrame();调用以下内容:

Switch To Parent Frame

切换到父框架

Where, the Switch to Parent Framecommand sets the current browsing contextfor future commands to the parentof the current browsing context.

其中,在切换到父框架命令设置当前浏览器上下文为未来的命令给父母的的当前浏览器上下文

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

根据Java DocsparentFrame()方法将焦点更改为父上下文。如果当前上下文是顶级浏览上下文,则上下文保持不变。

driver.switchTo().defaultContent();

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.

根据Java DocsdefaultContent()当页面包含 iframe 时,方法选择页面上的第一帧或主文档。

回答by cruisepandey

There is clearly a difference :

明显有区别:

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

场景:当有多个框架并且其中一些是嵌套的时。

iframeMain
iframeParent
iframechild

iframeMain
iframeParent
iframechild

Assume you are in ifrmaechild:

假设您在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.

当你这样做时driver.switchTo().parentFrame();:你会去iframeParent
但是当您这样做时driver.switchTo().defaultContent();:您将转到页面的主 HTML。
请注意,在这种情况下,您不会转到iframeMain

回答by JaySin

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.

当你处理网页中的多个 iframe 时,driver.switchTo().parentFrame()通常用于将控件切换回父框架。

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.

当您处理网页中的弹出对话框窗口时, thendriver.switchTo().defaultContent()用于将控件切换回窗口中的默认内容。

回答by sandeep padhy

driver.switchTo().defaultContent();

driver.switchTo().defaultContent();

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

这会将控制传递给包含 iframe 的主文档

driver.switchTo().parentFrame();

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

现在使用 driver.switchTo().defaultContent(); 将控制权交给主体

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

并使用 driver.switchTo().parentFrame(); 将控制权传递给 frame1 。