Monday, 10 September 2018

What is the difference between switchTo().defaultContent() and switchTo().parentFrame() in Selenium


switchTo().defaultContent() - will switch back to main HTML.

Where as, switchTo().parentFrame() switches to the immediate parent frame.

Let us understand through example. Below is the sample structure of html where frames are embedded inside each other. frame2 is available inside frame1 and frame3 available inside frame2.
frame3 contains dropdown1 and frame4 contains dropdown2.

frame1
     frame2
         frame3
                   select dropdown1
         frame4
                   select dropdown2



Now to work on first dropdown and then second dropdown this would be the code.

driver.switchTo().frame("frame1");
driver.switchTo().frame("frame2");
driver.switchTo().frame("frame3");
// select dropdown1 code

driver.switchTo().parentFrame(); // switched to frame2 frame
driver.switchTo().frame("frame4");
// select dropdown2 code

if switchTo().parentFrame();  not used, this would be the code:

driver.switchTo().frame("frame1");
driver.switchTo().frame("frame2");
driver.switchTo().frame("frame3");
// select dropdown1 code

driver.switchTo().defaultFrame(); // switched to main html
driver.switchTo().frame("frame1");
driver.switchTo().frame("frame2");
driver.switchTo().frame("frame4");
// select dropdown2 code

So this way, by using switchTo().parentFrame() it saves few lines of code compared to using defaultFrame() all the time.


Hope this blog has been useful to you.

Youtube channel: https://www.youtube.com/user/srinivaskinik
Facebook page: https://www.facebook.com/srinivaskinikalmady/


No comments:

Post a Comment

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule 1. Create a batch file with following content ...