SDET- QA Automation Techie

Software Testing Blog

  • Home
  • Training
    • Online
    • Self-Paced
  • Video Tutorials
  • Interview Skills
    • HR Interview Questions Videos
    • Domain Knowledge
  • Career Guidance
  • Home
  • Software Testing
    • Manual Testing Tutorials
    • Manual Testing Project
    • Manaul Testing FAQS
    • ISTQB
    • AGILE
  • Web Automation Testing
    • Java Programmng
    • Python Programmng
    • Selenium with Java
    • Selenium with Python
    • Robot Framework(Selenium with Python)
    • selenium with Cucumber
    • TestNG+IntelliJ
    • Mobile App Testing(Appium)
    • JMeter
  • API Automation Testing
    • Rest Assured API Testing (BDD)
    • Rest Assured API Testing (Java+ TestNG)
    • Robot Framework(Rest API Testing with Python)
    • Postman
    • SoapUI
    • API Testing(FAQ's)
  • SDET|DevOps
    • Continuos Integration
    • SDET Essentials
    • AWS For Testers
    • Docker
  • SQL
    • Oracle(SQL)
    • MySQL for Testers
    • NoSQL
  • Unix/Linux
    • UNIX TUTORIALS
    • Linux Shell Scripting
  • ETL Testing
    • ETL Data warehouse Tutorial
    • ETL Concepts Tools and Templates
    • ETL Testing FAQ's
    • ETL Testing Videos
  • Big Data Hadoop
  • Video Tutorials
  • ApachePOI Video Tutorials
  • Downloads
    • E-Books for Professionals
    • Resumes
  • Automation Essencials
    • Cloud Technologies
      • Docker For Testers
      • AWS For Testers
      • Sub Child Category 3
    • Java Collections
    • Selenium Locators
    • Frequently Asked Java Programs
    • Frequently Asked Python Programs
    • Protractor
    • Cypress Web Automation

Working with Frames in Selenium Webdriver

 Working with Frames in Selenium Webdriver   

What is iFrame? An iFrame (Inline Frame) is an HTML document embedded inside the current HTML document on a website. iFrame HTML element is used to insert content from another source, such as an advertisement, into a Web page. A Web designer can change an iFrame's content without making them reload the complete website. A website can have multiple frames on a single page. And a frame can also have inner frames (Frame in side a Frame)






 ---------------------------------------
|              |                             |
|              |                             |
| Frame 1 |                             |
|              |                             |
|              |                             |
|--------------|                             |
|              |          Frame 3       |
|              |                             |
|              |                             |
|              |                             |
| Frame 2 |                             |
|              |                             |
|              |                             |
|              |                             |
|              |                             |
 --------------------------------------------
In Selenium to work with iFrames, we have different ways to handle frame depending on the need. Please look at the below ways of handling frames
driver.switchTo().frame(int arg0);
Select a frame by its (zero-based) index. That is, if a page has multiple frames (more than 1), the first frame would be at index "0", the second at index "1" and so on.
Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame. i.e the driver focus will be now on the frame. What ever operations we try to perform on pages will not work and throws element not found as we navigated / switched to Frame.
Parameters: Index - (zero-based) index
Returns: driver focused on the given frame (current frame)
Throws: NoSuchFrameException - If the frame is not found.
Example: if iframe id=webklipper-publisher-widget-container-frame, it can be written as driver.switchTo().frame("webklipper-publisher-widget-container-frame"); Below is the code snippet to work with switchToFrame using frame id.
public void switchToFrame(int frame) {
  try {
   driver.switchTo().frame(frame);
   System.out.println("Navigated to frame with id " + frame);
  } catch (NoSuchFrameException e) {
   System.out.println("Unable to locate frame with id " + frame
     + e.getStackTrace());
  } catch (Exception e) {
   System.out.println("Unable to navigate to frame with id " + frame
     + e.getStackTrace());
  }
 }
driver.switchTo().frame(String arg0);
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
Parameters: name Or Id - the name of the frame or the id of the frame element.
Returns: driver focused on the given frame (current frame)
Throws: NoSuchFrameException - If the frame is not found
Below is the example code snippet using frame name.
 
public void switchToFrame(String frame) {
  try {
   driver.switchTo().frame(frame);
   System.out.println("Navigated to frame with name " + frame);
  } catch (NoSuchFrameException e) {
   System.out.println("Unable to locate frame with id " + frame
     + e.getStackTrace());
  } catch (Exception e) {
   System.out.println("Unable to navigate to frame with id " + frame
     + e.getStackTrace());
  }
 }
driver.switchTo().frame(WebElement frameElement);
Select a frame using its previously located WebElement.
Parameters: frameElement - The frame element to switch to.
Returns: driver focused on the given frame (current frame).
Throws: NoSuchFrameException - If the given element is neither an iframe nor a frame element. And StaleElementReferenceException - If the WebElement has gone stale.
Below is the example code to send an Element to the and switch.
public void switchToFrame(WebElement frameElement) {
  try {
   if (isElementPresent(frameElement)) {
    driver.switchTo().frame(frameElement);
    System.out.println("Navigated to frame with element "+ frameElement);
   } else {
    System.out.println("Unable to navigate to frame with element "+ frameElement);
   }
  } catch (NoSuchFrameException e) {
   System.out.println("Unable to locate frame with element " + frameElement + e.getStackTrace());
  } catch (StaleElementReferenceException e) {
   System.out.println("Element with " + frameElement + "is not attached to the page document" + e.getStackTrace());
  } catch (Exception e) {
   System.out.println("Unable to navigate to frame with element " + frameElement + e.getStackTrace());
  }
 }
Some times when there are multiple Frames (Frame in side a frame), we need to first switch to the parent frame and then we need to switch to the child frame. below is the code snippet to work with multiple frames.
public void switchToFrame(String ParentFrame, String ChildFrame) {
  try {
   driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
   System.out.println("Navigated to innerframe with id " + ChildFrame
     + "which is present on frame with id" + ParentFrame);
  } catch (NoSuchFrameException e) {
   System.out.println("Unable to locate frame with id " + ParentFrame
     + " or " + ChildFrame + e.getStackTrace());
  } catch (Exception e) {
   System.out.println("Unable to navigate to innerframe with id "
     + ChildFrame + "which is present on frame with id"
     + ParentFrame + e.getStackTrace());
  }
 }
After working with the frames, main important is to come back to the web page. if we don't switch back to the default page, driver will throw an exception. Below is the code snippet to switch back to the default content.
public void switchtoDefaultFrame() {
  try {
   driver.switchTo().defaultContent();
   System.out.println("Navigated back to webpage from frame");
  } catch (Exception e) {
   System.out
     .println("unable to navigate back to main webpage from frame"
       + e.getStackTrace());
  }
 }
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home
popup

Popular Posts

  • How To Explain Project In Interview Freshers and Experienced
    “ Describe an important project you’ve worked on ” is one of the most common questions you can expect in an interview. The purpose of a...
  • MANUAL TESTING REAL TIME INTERVIEW QUESTIONS & ANSWERS
    1. How will you receive the project requirements? A. The finalized SRS will be placed in a project repository; we will access it fr...
  • API/Webservices Testing using RestAssured (Part 1)
    Rest Assured : Is an API designed for automating REST services/Rest API's Pre-Requisites Java Free videos: https://www.you...

Facebook Page

Pages

  • Home
  • Resumes
  • Job Websites India/UK/US
  • ISTQB
  • Selenium with Java
  • E-Books for Professionals
  • Manual Testing Tutorials
  • Agile Methodology
  • Manual Testing Projects

Live Traffic

YouTube


Blog Visitors

Copyright © SDET- QA Automation Techie | Powered by Blogger
Design by SDET | Blogger Theme by | Distributed By Gooyaabi Templates