C# 从 iFrame 切换到父框架并使用 Selenium Webdriver 在父框架中查找元素。C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17496010/
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
Switching to Parent Frame from iFrame and finding an element in Parent frame using Selenium Webdriver. C#
提问by Stand4Unborn
Scenario: - I have a page with an iFrame Text Editor and a button in the page too. - I switched from the parent frame to the iFrame to read from the Text Editor body - After reading from the body of the Text Editor, I want to click on the button in the parent frame of the page. - For this I tried to switch back to the parent frame from the iFrame using the following statement: webDriver.SwitchTo().DefaultContent(); - But still I am not able to find the button element which resides in the parent frame.
场景: - 我有一个带有 iFrame 文本编辑器的页面,页面中也有一个按钮。- 我从父框架切换到 iFrame 以从文本编辑器正文读取 - 从文本编辑器正文读取后,我想单击页面父框架中的按钮。- 为此,我尝试使用以下语句从 iFrame 切换回父框架: webDriver.SwitchTo().DefaultContent(); - 但我仍然无法找到驻留在父框架中的按钮元素。
I appreciate your help! Thanks
我感谢您的帮助!谢谢
采纳答案by Stand4Unborn
Thats for your responses guys. It is solved!
那是你们的回应。解决了!
The solution:
解决方案:
While I use the
webDriver.SwitchTo().DefaultContent();it switches thewebDriverto the top most window of the page. [Previously I was looking for the button element in this window and therefore was not able to find it as the button was sitting in the main frame of the page]After switching to the main window, I switched the
webDriveragain to the main frame of the page. This main frame had the button element. Thus I was able to find the button element. And this slved the issue!
当我使用
webDriver.SwitchTo().DefaultContent();它时,它会切换webDriver到页面的最顶部窗口。[以前我在这个窗口中寻找按钮元素,因此无法找到它,因为按钮位于页面的主框架中]切换到主窗口后,我
webDriver再次切换到页面的主框架。这个主框架有按钮元素。因此我能够找到按钮元素。这解决了问题!
So the final code doesn't have webDriver.SwitchTo().DefaultContent();but has the following in its place:
所以最终的代码没有webDriver.SwitchTo().DefaultContent();但在它的位置有以下内容:
_webDriver.SwitchTo().Window(windowHandle);
_webDriver.SwitchTo().Frame("mainFrame");
Note: windowHandle in the above code is the handle of the top most window of the page. I guess it's value may change according to the browsers, not sure though.
注意:上面代码中的windowHandle是页面最上面窗口的句柄。我猜它的值可能会根据浏览器而变化,但不确定。
回答by DevDave
I was struggling with a similar problem and found that I could switch back by Window Handle:
我正在努力解决类似的问题,发现我可以通过Window Handle以下方式切换回来:
string currentWindow = Driver.CurrentWindowHandle;
// switch to frame and do stuff..
Driver.SwitchTo().Window(currentWindow); // switch back
回答by Shivam Bharadwaj
In my case the following code worked fine:
在我的情况下,以下代码工作正常:
driver.switchTo().parentFrame();

