selenium - is it possible to know if we are already inside a iframe -


in application of elements inside iframe.

so pom (page object model) methods start switching frame , code performing actions..am able perform action switching frame.

below example of code:

public void method 1() {   driver.switchto().frame(0);   // code perform actions....   method 2();   driver.switchto().defaultcontent(); }  public void method 2() {   driver.switchto().frame(0);   // code perform actions.... } 

as per above example, 2nd method called 1st method

the driver frame method 1, when method 2 called again tried switch frame 0, hardcoded frame index, thought work fine (i.e driver on same frame), giving error

"no such frame exception."

is possible know current frame? if know frame can add condition , decide switch or not switch, please guide.

you achieve creating global variable current switched frame below approach :-

string currentframe = null; //make currentframe global variable   public void switchtoframe(string frame) {   if ((null != frame) && (!"".equals(frame))) {      if (!frame.equals(currentframe)) {         driver.switchto().defaultcontent();         driver.switchto().frame(frame);         currentframe = frame;      }  } else {     currentframe = "";     driver.switchto().defaultcontent();  } } 

now can use in methods below :-

public void method 1() {   switchtoframe("your frame id or name"); //pass null if want switch default   // code perform actions....   method 2(); }  public void method 2() {   switchtoframe("your frame id or name"); //pass null if want switch default   // code perform actions.... } 

note : - if not want yo create global variable know current frame, can use javascriptexecutor know current frame below :-

javascriptexecutor jsexecutor = (javascriptexecutor)driver; string currentframe = jsexecutor.executescript("return self.name"); 

hope you...:)


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -