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.
Now I need to improve it's performance by following scnario:
requested_frame=currentFrame
then No need to switchrequested_frame=null
then switch to default (if currentFrame=default
then again no need to switch)requested_frame!=null && requested_frame!=currentFrame
then switch to default, and switch to requested_frame
frame.So all I need now is to get the Current Frame. Is there any way I can get it?
To move back to the parent frame, you can either use switchTo(). parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo(). defaultContent();
parentFrame() is generally used to switch the control back to the parent frame. 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.
Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.
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");
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 :-)
I Just did a small test, with my current version, with a global variable and with the js command help.
[Time collected (ms) on perfoming actions in 4 different elements in 2 nested iframes 1000 times. (4000 find element actions in total) ]
| | 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With