Selenium WebDriver在切换之前获取当前帧 [英] Selenium WebDriver get Current frame before switch
问题描述
通过切换某个iFrame,我有一个自定义操作来查找元素。
它使用以下原生动作:
driver.switchTo()。frame(requested_frame)
目前我正在切换回默认值在切换到确切的iframe之前每次都有内容。
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.
现在我需要通过以下scnario来提高它的性能:
Now I need to improve it's performance by following scnario:
- 检查当前内容/框架
- 如果
requested_frame = currentFrame
则无需切换 - if
requested_frame = null
然后切换到默认值(如果currentFrame = default
然后再次如果requested_frame!= null&& requested_frame!= currentFrame
然后切换到默认值,并切换到requested_frame
frame。
- 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.
所以我现在需要的是获得当前帧。有什么方法可以得到它吗?
So all I need now is to get the Current Frame. Is there any way I can get it?
推荐答案
作为我自己问题的确切答案Selenium WebDriver获取当前帧我找到了以下方法。
As the exact answer for my own question "Selenium WebDriver get Current frame" I found the following way to do it.
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
String currentFrame = jsExecutor.executeScript("return self.name");
但是在这里,JavascriptExecutor每次执行命令以获取当前帧。
因此,如果我考虑性能,我想选择# Saurabh Gaur 的答案作为最佳答案 。
我希望我的回答对其他人有帮助: - )
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 :-)
我刚用当前版本进行了一次小测试,使用全局变量和js命令帮助。
I Just did a small test, with my current version, with a global variable and with the js command help.
[在2个嵌套iframe中的4个不同元素中执行动作的时间(ms)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 |
所以我的每次切换默认值的当前表现都不差:D
So the current performance with my "Switching default everytime" is not bad at all :D
这篇关于Selenium WebDriver在切换之前获取当前帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!