Your ultimate guide

Handling iframe/frame using Java with Selenium

Frames:
    In Selenium, 'frames' refer to HTML '<iframe>' elements on a web page. Frames are divide your browser window into multiple sections, and they are often used for things like displaying advertisements or loading content from external source.
    Example HTML source code for frames in a web page:
<iframe src="https://example.com" name="framename" id="framename" title="frametitle">
----
</iframe>

 

You may need to interact with elements inside frames. Here's how you can work with frames in Selenium:
  1. switchTo().frame()
  2. switchTo().defaultContent()
  3. switchTo().parentFrame()




1.  switchTo().frame():
    When a web element is located in an iframe tag in that situation first we need to switch into that frame then only we can access that web element.
    To change the web driver object from page level to frame level we will use switchTo().frame() method.

We can access the frame by using either the frame name, index or web element.
  1. By name:
    • If a frame has a 'name' or 'id' attribute, you can switch to it by specifying those attribute values.
    • Syntax: driver.switchTo().frame(frameName);
    • Example: Source Code: <iframe src="https://example.com" id="packageListFrame" name="frame1">
      • 1) driver.switchTo().frame(“packageListFrame”);
      • 2) driver.switchTo().frame(“frame1”)
  2. By Index:
    • Switching by index refers to the act of switching focus to specific frame using its index within the list of frames on a web page. Frames in HTML are numbered starting from 0. The index represents the position of the frame within the frameset or iframe tags. 
    • Syntax: driver.switchTo().frame(index number);
    • Example: driver.switchTo().frame(0);
  3. By Web Element:
    • If you have a reference to a WebElement representing the frame, you can switch to it.
    • Syntax: driver.switchTo().frame(WebElement);
    • Example:
      1. WebElement frame_element = driver.findElement(By.id(“q”));
      2. driver.switchTo().frame(frame_element);
 


 
2.  switchTo().defaultContent():
    We can use this method to change the web driver object frame level to the page level which means switching back to the main content from a frame.

    Example/Syntax: driver.switchTo().defaultContent();
 


3.  switchTo().parentFrame():
    This method is used to change focus from the current frame to the parent frame (Exit from the current frame and enter the previous frame).

    Example/Syntax: driver.switchTo().parentFrame();




Example Program:
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.chrome.ChromeDriver;

  5. public class FramesExample {
  6.   public static void main(String[] args) {
  7.     WebDriver driver = new ChromeDriver();
  8.     driver.get("https://testingcolleges.blogspot.com/p/testingframes.html");
  9.     
  10.     driver.switchTo().frame("demopckg");
  11.     WebElement userName = driver.findElement(By.id("001"));
  12.     userName.sendKeys("Testing");
  13.     
  14.     driver.switchTo().frame("bookmark");
  15.     WebElement password = driver.findElement(By.id("002"));
  16.     password.sendKeys("Colleges");
  17.     
  18.     driver.switchTo().parentFrame();
  19.     userName.clear();
  20.     
  21.     driver.switchTo().defaultContent();
  22.     WebElement refreshPage = driver.findElement(By.id("gjkl"));
  23.     refreshPage.click();
  24.     driver.close();
  25.   }
  26. }



No comments:

Post a Comment