java Selenium WebDriver 在切换前获取当前帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37791547/
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
Selenium WebDriver get Current frame before switch
提问by Tharaka Deshan
I have a custom action to find elements, by switching in the certain iFrame.
It uses the following native action:
driver.switchTo().frame(requested_frame)
Currently I am switching back to the default content every-time before switch to the exact iframe.
我有一个自定义操作来查找元素,方法是在某个 iFrame 中切换。它使用以下本机操作:
driver.switchTo().frame(requested_frame)
目前,在切换到确切的 iframe 之前,我每次都切换回默认内容。
Now I need to improve it's performance by following scnario:
现在我需要通过以下 snario 来提高它的性能:
- Check the current content/frame
- if
requested_frame=currentFrame
then No need to switch - if
requested_frame=null
then switch to default (ifcurrentFrame=default
then again no need to switch) - if
requested_frame!=null && requested_frame!=currentFrame
then switch to default, and switch torequested_frame
frame.
- 检查当前内容/框架
- 如果
requested_frame=currentFrame
那么 不需要切换 - if
requested_frame=null
then switch to default (ifcurrentFrame=default
then again不需要切换) - 如果
requested_frame!=null && requested_frame!=currentFrame
然后切换到默认,并切换到requested_frame
框架。
So all I need now is to get the Current Frame. Is there any way I can get it?
所以我现在需要的就是获取当前帧。有什么办法可以得到吗?
回答by Tharaka Deshan
As the exact answer for my own question "Selenium WebDriver get Current frame"I found the following way to do it.
作为我自己的问题“Selenium WebDriver 获取当前框架”的确切答案,我找到了以下方法。
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
String currentFrame = jsExecutor.executeScript("return self.name");
But in here, the JavascriptExecutor execute the command every time to get the current frame. So if I consider the performance, I would like to choose #Saurabh Gaur's answer as the best answer . I hope my answer would be helpful to someone else :-)
但是在这里,JavascriptExecutor 每次都执行命令来获取当前帧。所以如果考虑性能,我想选择#Saurabh Gaur的答案作为最佳答案。我希望我的回答对其他人有帮助:-)
Adding later
稍后添加
I Just did a small test, with my current version, with a global variable and with the js command help.
我刚刚用我当前的版本、一个全局变量和 js 命令帮助做了一个小测试。
[Time collected (ms) on perfoming actions in 4 different elements in 2 nested iframes 1000 times. (4000 find element actions in total)]
[在 2 个嵌套 iframe 中的 4 个不同元素中执行操作的时间收集(毫秒)1000 次。 (总共 4000 个查找元素动作)]
| | Switch everytime | Global variable | JS command |
|----------|------------------|-----------------|------------|
| | 2023 | 1950 | 2911 |
| | 1902 | 2091 | 2992 |
| | 2014 | 1974 | 3020 |
| | 1934 | 1931 | 3097 |
| | 1997 | 1965 | 3180 |
|----------|------------------|-----------------|------------|
| Average | 1974 | 1982.2 | 3040 |
So the current performance with my "Switching default everytime" is not bad at all :D
因此,我的“每次都切换默认值”的当前性能一点也不差:D
回答by Saurabh Gaur
You need to implement as below approach :-
您需要实施以下方法:-
String frame = 'your frame';
String currentFrame = null;
// make this currentFrame as global
if ((null != frame) && (!"".equals(frame))) {
if (!frame.equals(currentFrame)) {
switchToFrame(frame);
currentFrame = frame;
}
} else {
currentFrame = "";
webDriver.switchTo().defaultContent();
}
public void switchToFrame(String frame) {
webDriver.switchTo().defaultContent();
webDriver.switchTo().frame(frame);
}
Hope it will handle your all conditions..:)
希望它能处理你的所有条件..:)