Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver get Current frame before switch

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:

  • Check the current content/frame
  • if requested_frame=currentFrame then No need to switch
  • if requested_frame=null then switch to default (if currentFrame=default then again no need to switch)
  • if 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?

like image 841
Tharaka Deshan Avatar asked Jun 13 '16 13:06

Tharaka Deshan


People also ask

How do I switch to previous frame in Selenium?

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();

What is difference between parentFrame () and defaultContent () methods?

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.

What does the switchTo () command do in Selenium?

Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.


1 Answers

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 :-)


Adding later

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

like image 182
Tharaka Deshan Avatar answered Sep 25 '22 09:09

Tharaka Deshan