Monday 1 February 2016

Handling iframes in selenium

Basically, we can switch over the elements in frames using 3 ways.

By Index
By Name or Id
By Web Element
Switch to the frame by index:

Index is one of the attributes for the Iframe through which we can switch to it.

Index of the iframe starts with '0'.

Suppose if there are 100 frames in page, we can switch to the iframe by using index.

driver.switchTo().frame(0);
driver.switchTo().frame(1);
Switch to the frame by Name or ID:

Name and ID are attributes of iframe through which we can switch to the it.

driver.switchTo().frame("iframe1");
driver.switchTo().frame("id if the element");
Example of Switching to iframe through ID:
===========================================


public class SwitchToFrameDemo {
public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver(); //navigates to the Browser
        driver.get("");
           // navigates to the page consisting an iframe

           driver.manage().window().maximize();
           driver.switchTo().frame("ramesh"); //switching the frame by ID

            System.out.println("********We are switch to the iframe*******");
            driver.findElement(By.xpath("html/body/a/img")).click();
            //Clicks the iframe
       
            System.out.println("");
      }

No comments:

Post a Comment