How to Handle Frames in Selenium WebDriver
H Y R Tutorials H Y R Tutorials
63.6K subscribers
59,777 views
774

 Published On May 28, 2020

In this video, I have shown how we can handle Frames in Selenium WebDriver.

Frames are a now deprecated means of building a site layout from multiple documents on the same domain. You are unlikely to work with them unless you are working with a pre-HTML5 web app. Iframes allow the insertion of a document from an entirely different domain and are still commonly used.

To interact with any control inside the frame, we will need to first switch to the frame, in a similar way to how we switch windows. WebDriver offers three ways of switching to a frame.
1) Using an index
2) Using a name or ID
3) Using a WebElement

----------- Using an index: -----------------
It is possible to use the index of the frame to switch to that frame, such as can be queried using window.frames in JavaScript.
Here the index starts from 0(zero)

Example:
// Switches to the second frame
driver.switchTo().frame(1);

----------- Using a name or ID -----------------
If your frame or iframe has an id or name attribute, this can be used instead. If the name or ID is not unique on the page, then the first one found will be switched to.

Example:
//Using the ID
driver.switchTo().frame("testframe");

//Or using the name instead
driver.switchTo().frame("frm1");

//Now we can click the button
driver.findElement(By.tagName("button")).click();

----------- Using a WebElement -----------------
Switching using a WebElement is the most flexible option. You can find the frame using your preferred selector and switch to it.

Example:
//Store the web element
WebElement iframe = driver.findElement(By.xpath("//h1[.='Frame1']/following-sibling::iframe"));

//Switch to the frame
driver.switchTo().frame(iframe);

//Now we can click the button
driver.findElement(By.tagName("button")).click();


-------------- Leaving a frame -----------------------
To leave an iframe or frameset, switch back to the default content like so:

Example:
// Return to the top level
driver.switchTo().defaultContent();

--------------- Going to parent frame -----------------
When you are in nested frames and you want to come back to the parent frame of all the child frames then we use a method called parentframe.

Example:
driver.switchTo().frame("testframe1");
driver.switchTo().frame("testChildFrame1");
driver.switchTo().frame("testChildFrame11");

//Return to parent frame
driver.switchTo.parentFrame();

==============================================
************* Checkout my other playlists *************
==============================================
Java Programming videos playlist:πŸ‘‡
πŸ”— https://bit.ly/3keRJGa

Selenium WebDriver with Java videos playlist:πŸ‘‡
πŸ”— https://bit.ly/2FyKvxj

Selenium interview questions videos playlist:πŸ‘‡
πŸ”— https://bit.ly/3matUB3

Windows automation with FlaUI videos playlist:πŸ‘‡
πŸ”— https://bit.ly/33CG4dB

CSS Selectors videos playlist:πŸ‘‡
πŸ”— https://bit.ly/2Rn0IbD

XPath videos playlist:πŸ‘‡
πŸ”— https://bit.ly/2RlLdkw

Javascript Executor videos playlist:πŸ‘‡
πŸ”— https://bit.ly/2FhNXwS

Apache POI videos playlist:πŸ‘‡
πŸ”— https://bit.ly/2RrngrH

Maven videos playlist:πŸ‘‡
πŸ”— https://bit.ly/2DYfYZE

How to fix Eclipse errors:πŸ‘‡
πŸ”— https://bit.ly/3ipvNYf
==============================================
==============================================
Connect us @
πŸ”— Website - www.hyrtutorials.com
πŸ”— Facebook - www.facebook.com/HYRTutorials
πŸ”— LinkedIn - www.linkedin.com/company/hyrtutorials
πŸ”— Twitter - www.twitter.com/Hyrtutorials
==============================================
==============================================
πŸ™ Please SubscribeπŸ”” to start learning for FREE now, Also help your friends in learning the best by suggesting this channel.

#webAutomation #selenium #frames #hyrtutorials

show more

Share/Embed