我有一个自定义操作来查找元素,方法是切换到特定的 iFrame。
它使用以下 native 操作:
driver.switchTo().frame(requested_frame)
目前,在切换到确切的 iframe 之前,我每次都切换回默认内容。
现在我需要通过以下方案来提高它的性能:
- 检查当前内容/框架
- 如果
requested_frame=currentFrame
那么不需要切换 - 如果
requested_frame=null
则切换到默认值(如果currentFrame=default
则无需切换) - 如果
requested_frame!=null && requested_frame!=currentFrame
则切换到默认,并切换到requested_frame
框架。
所以我现在需要的是获取当前帧。有什么办法可以得到它吗?
最佳答案
作为我自己的问题“Selenium WebDriver get Current frame”的确切答案,我找到了以下方法。
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
String currentFrame = jsExecutor.executeScript("return self.name");
但在这里,JavascriptExecutor 每次都执行命令以获取当前帧。 所以如果我考虑性能的话,我会选择#Saurabh Gaur的答案为最佳答案。 我希望我的回答对其他人有帮助:-)
稍后添加
我刚刚用我当前的版本做了一个小测试,有一个全局变量和 js 命令帮助。
[在 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 |
所以我的“每次切换默认值”的当前性能一点都不差:D
关于java - Selenium WebDriver 在切换前获取当前帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791547/