SlideShare a Scribd company logo
Handling iFrames using
Selenium Webdriver
IFrames
An IFrame (Inline Frame) is an HTML document embedded inside another HTML
document on a website. The IFrame HTML element is often used to insert content from
another source, such as an advertisement, into a Web page. Although an IFrame behaves
like an inline image, it can be configured with its own scrollbar independent of the
surrounding page's scrollbar.
We will learn -
1. How do switch over the elements in iframes using WebDriver.
2. Nested Frames (Frames inside Frames)
How do switch over
the elements in
iframes using
WebDriver.
To Switch between iFrames
we have to use the driver’s
switchTo().frame()
command. After switching to
an Iframe we will be able to
work on web elements.
Basically, We can use the switchTo().frame() in three
ways to access the iframe.
● By Index
● By Name or Id
● By Web Element
<html><body>
<div class=”row”>
<iframe name="iframe1" id="Frame1" height="600" width="400"
src="https://www.facebook.com/"></iframe>
</div>
<div class=”row”>
<iframe name="iframe2" id="Frame2" height="600" width="400"
src="youtube.com”></iframe>
</div>
</body></html>
Sample Page 1
Switching By Index:
Syntax: switchTo.frame(frameindex);
Index of an iFrame is the position at which it
occurs in the HTML page.
Index of the iframe starts with '0'.
If we have two Iframes, Index 0 will be the
iFrame which exists earlier in the HTML page.
E.g
Switching to the First IFrame
driver.switchTo().frame(0);
Switching to the Second IFrame
driver.switchTo().frame(1);
Switching By Name / ID:
Syntax: switchTo.frame(“framename or frameID”);
We can also switch to iframes using values of
their name attribute or ID attribute.
E.g
Switching to an iframe using name attribute
driver.switchTo().frame("iframe1");
Switching to an iframe using ID attribute
driver.switchTo().frame("Frame1");
Switching by WebElement:
Syntax: switchTo.frame(iframelocator);
We can switch to an iFrame by simply passing
the iFrame WebElement.
First, we need to find the iFrame element using
any of the locator strategies and then pass it to
switchTo.frame() command.
E.g
Finding webelement by ID locator
WebElement iframeElement =
driver.findElement(By.id("Frame1"));
Switching to an iframe using webelement
driver.switchTo().frame(iframeElement);
How to switch back to
the Main Page or Main
Frame
After doing all the operation
to iframe element, we need
to get back to the main page.
As in our sample page, the
main page is the page in
which contains two iFrames.
Once we are done with all
the task in a particular
iFrame you can switch back
to the main page.
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();
E.g
Switching to the First IFrame
driver.switchTo().frame(0);
Do all the required tasks in the frame 0
driver.findElement(//*@id=’activate_key’]).sendKeys(“AS9g”);
Switch back to the main window
driver.switchTo().defaultContent();
<html><body>
<div class=”row”>
<iframe name="iframe1" id="Frame1" src="www.facebook.com">
<iframe name="iframe2" id="Frame2" height="600" width="400"
src="youtube.com”>
</iframe>
</iframe>
</div>
</body></html>
Sample Page 2
Nested Frames
(Frames inside
Frames)
In case we have a web page
that contains two frames
one inside other like shown
in Sample Page 2 and we
need to perform some
operations.
1. To begin with, We must switch to the outer
frame(iframe1) by any of the known methods.
2. After switching to the outer frame(iframe1) we can
find the total number of iframes inside the outer
frame(iframe1).
driver.findElements(By.tagName("iframe")).size();
3. We can switch to the inner frame(iframe2) by any of
the known methods and perform the desired
operation.
While exiting out of the frame, we must exit out in the
same order as we entered into it from the inner
frame(iframe2) first and then outer frame(iframe1).
Now we know how to handle Iframes
using Selenium Webdriver
Thank You
Pankaj Biswas

More Related Content

What's hot

Qtp Basics
Qtp BasicsQtp Basics
Qtp Basics
mehramit
 

What's hot (20)

Qtp Basics
Qtp BasicsQtp Basics
Qtp Basics
 
Basic Guide to Manual Testing
Basic Guide to Manual TestingBasic Guide to Manual Testing
Basic Guide to Manual Testing
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | Edureka
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Bug life cycle
Bug life cycleBug life cycle
Bug life cycle
 
Page Object Model and Implementation in Selenium
Page Object Model and Implementation in Selenium  Page Object Model and Implementation in Selenium
Page Object Model and Implementation in Selenium
 
Selenium - Introduction
Selenium - IntroductionSelenium - Introduction
Selenium - Introduction
 
Automation Testing using Selenium
Automation Testing using SeleniumAutomation Testing using Selenium
Automation Testing using Selenium
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
Agile_basics
Agile_basicsAgile_basics
Agile_basics
 
(편집-테스트카페 발표자료) 1인 QA 수행사례로 발표한 자료 (W프로젝트 사례)
(편집-테스트카페 발표자료) 1인 QA 수행사례로 발표한 자료 (W프로젝트 사례)(편집-테스트카페 발표자료) 1인 QA 수행사례로 발표한 자료 (W프로젝트 사례)
(편집-테스트카페 발표자료) 1인 QA 수행사례로 발표한 자료 (W프로젝트 사례)
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Software Testing Life Cycle – A Beginner’s Guide
Software Testing Life Cycle – A Beginner’s GuideSoftware Testing Life Cycle – A Beginner’s Guide
Software Testing Life Cycle – A Beginner’s Guide
 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDD
 
Manual Testing Notes
Manual Testing NotesManual Testing Notes
Manual Testing Notes
 
Waits in Selenium | Selenium Wait Commands | Edureka
Waits in Selenium | Selenium Wait Commands | EdurekaWaits in Selenium | Selenium Wait Commands | Edureka
Waits in Selenium | Selenium Wait Commands | Edureka
 
Selenium Automation Framework
Selenium Automation  FrameworkSelenium Automation  Framework
Selenium Automation Framework
 
What is sanity testing
What is sanity testingWhat is sanity testing
What is sanity testing
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 

Similar to Handling iframes using selenium web driver

Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
sotlsoc
 
Java script performance tips
Java script performance tipsJava script performance tips
Java script performance tips
Shakti Shrestha
 

Similar to Handling iframes using selenium web driver (20)

Handling iFrames in Selenium Based Test Automation.pdf
Handling iFrames in Selenium Based Test Automation.pdfHandling iFrames in Selenium Based Test Automation.pdf
Handling iFrames in Selenium Based Test Automation.pdf
 
Css, xhtml, javascript
Css, xhtml, javascriptCss, xhtml, javascript
Css, xhtml, javascript
 
Selenium interview questions and answers
Selenium interview questions and answersSelenium interview questions and answers
Selenium interview questions and answers
 
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
 
Web components
Web componentsWeb components
Web components
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
Do you want to contribute to volto?
Do you want to contribute to volto?Do you want to contribute to volto?
Do you want to contribute to volto?
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Java script performance tips
Java script performance tipsJava script performance tips
Java script performance tips
 
Final_Frames.pptx
Final_Frames.pptxFinal_Frames.pptx
Final_Frames.pptx
 
Meebo performance ny_web_performance
Meebo performance ny_web_performanceMeebo performance ny_web_performance
Meebo performance ny_web_performance
 
Live session 2 lightning web component
Live session 2 lightning web componentLive session 2 lightning web component
Live session 2 lightning web component
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learning
 
Simple web browser
Simple web browserSimple web browser
Simple web browser
 
React workshop
React workshopReact workshop
React workshop
 
react-en.pdf
react-en.pdfreact-en.pdf
react-en.pdf
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 
Silverlight Databinding
Silverlight DatabindingSilverlight Databinding
Silverlight Databinding
 

Recently uploaded

Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 

Recently uploaded (20)

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 

Handling iframes using selenium web driver

  • 2. IFrames An IFrame (Inline Frame) is an HTML document embedded inside another HTML document on a website. The IFrame HTML element is often used to insert content from another source, such as an advertisement, into a Web page. Although an IFrame behaves like an inline image, it can be configured with its own scrollbar independent of the surrounding page's scrollbar. We will learn - 1. How do switch over the elements in iframes using WebDriver. 2. Nested Frames (Frames inside Frames)
  • 3. How do switch over the elements in iframes using WebDriver. To Switch between iFrames we have to use the driver’s switchTo().frame() command. After switching to an Iframe we will be able to work on web elements. Basically, We can use the switchTo().frame() in three ways to access the iframe. ● By Index ● By Name or Id ● By Web Element
  • 4. <html><body> <div class=”row”> <iframe name="iframe1" id="Frame1" height="600" width="400" src="https://www.facebook.com/"></iframe> </div> <div class=”row”> <iframe name="iframe2" id="Frame2" height="600" width="400" src="youtube.com”></iframe> </div> </body></html> Sample Page 1
  • 5. Switching By Index: Syntax: switchTo.frame(frameindex); Index of an iFrame is the position at which it occurs in the HTML page. Index of the iframe starts with '0'. If we have two Iframes, Index 0 will be the iFrame which exists earlier in the HTML page. E.g Switching to the First IFrame driver.switchTo().frame(0); Switching to the Second IFrame driver.switchTo().frame(1);
  • 6. Switching By Name / ID: Syntax: switchTo.frame(“framename or frameID”); We can also switch to iframes using values of their name attribute or ID attribute. E.g Switching to an iframe using name attribute driver.switchTo().frame("iframe1"); Switching to an iframe using ID attribute driver.switchTo().frame("Frame1");
  • 7. Switching by WebElement: Syntax: switchTo.frame(iframelocator); We can switch to an iFrame by simply passing the iFrame WebElement. First, we need to find the iFrame element using any of the locator strategies and then pass it to switchTo.frame() command. E.g Finding webelement by ID locator WebElement iframeElement = driver.findElement(By.id("Frame1")); Switching to an iframe using webelement driver.switchTo().frame(iframeElement);
  • 8. How to switch back to the Main Page or Main Frame After doing all the operation to iframe element, we need to get back to the main page. As in our sample page, the main page is the page in which contains two iFrames. Once we are done with all the task in a particular iFrame you can switch back to the main page. 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(); E.g Switching to the First IFrame driver.switchTo().frame(0); Do all the required tasks in the frame 0 driver.findElement(//*@id=’activate_key’]).sendKeys(“AS9g”); Switch back to the main window driver.switchTo().defaultContent();
  • 9. <html><body> <div class=”row”> <iframe name="iframe1" id="Frame1" src="www.facebook.com"> <iframe name="iframe2" id="Frame2" height="600" width="400" src="youtube.com”> </iframe> </iframe> </div> </body></html> Sample Page 2
  • 10. Nested Frames (Frames inside Frames) In case we have a web page that contains two frames one inside other like shown in Sample Page 2 and we need to perform some operations. 1. To begin with, We must switch to the outer frame(iframe1) by any of the known methods. 2. After switching to the outer frame(iframe1) we can find the total number of iframes inside the outer frame(iframe1). driver.findElements(By.tagName("iframe")).size(); 3. We can switch to the inner frame(iframe2) by any of the known methods and perform the desired operation. While exiting out of the frame, we must exit out in the same order as we entered into it from the inner frame(iframe2) first and then outer frame(iframe1).
  • 11. Now we know how to handle Iframes using Selenium Webdriver Thank You Pankaj Biswas