Selenium
How to Download & Install TestNG in Eclipse for Selenium WebDriver
Following is a step by step guide to install TestNG in Eclipse Installing TestNG in Eclipse Step 1)...
iFrame in Selenium Webdriver is a web page or an inline frame which is embedded in another web page or an HTML document embedded inside another HTML document. The iframe is often used to add content from other sources like an advertisement into a web page. The iframe is defined with the <iframe> tag.
In this tutorial, you will learn -
We cannot detect the frames by just seeing the page or by inspecting Firebug.
Observe the below image, Advertisement being displayed is an Iframe, we cannot locate or recognize that by just inspecting using Firebug. So the question is how can you identify the iframe?
We can identify the frames in Selenium using methods given below:
In above diagram, you can see that 'This Frame' option is available upon right clicking, so we are now sure that it is an iframe.
We can even identify total number of iframes by using below snippet.
Int size = driver.findElements(By.tagName("iframe")).size();
Basically, we can switch over the elements and handle frames in Selenium using 3 ways.
Switch to the frame by index:
Index is one of the attributes for frame handling in Selenium 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 frame in Selenium by using index.
Switch to the frame by Name or ID:
Name and ID are attributes for handling frames in Selenium through which we can switch to the iframe.
Example of Switching to iframe through ID:
Let's take an example to switch frame in Selenium displayed in the below image. Our requirement is to click the iframe.
We can access this iframe through this below URL:http://demo.guru99.com/test/guru99home/
It is impossible to click iframe directly through XPath since it is an iframe. First we have to switch to the frame and then we can click using xpath.
Step 1)
WebDriver driver = new FirefoxDriver(); driver.get("http://demo.guru99.com/test/guru99home/"); driver.manage().window().maximize();
Step 2)
driver.switchTo().frame("a077aa5e");
Step 3)
driver.findElement(By.xpath("html/body/a/img")).click();
Here is the complete code:
public class SwitchToFrame_ID { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); //navigates to the Browser driver.get("http://demo.guru99.com/test/guru99home/"); // navigates to the page consisting an iframe driver.manage().window().maximize(); driver.switchTo().frame("a077aa5e"); //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("*********We are done***************"); } }
Output:
Browser navigates to the page consisting the above iframe and clicks on the iframe.
Switch to the frame by Web Element:
We can even switch to the iframe using web element .
How to switch back to the Main Frame
We have to come out of the iframe.
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();
driver.switchTo().parentFrame(); driver.switchTo().defaultContent();
How to switch over the frame, if we CANNOT switch using ID or Web Element:
Suppose if there are 100 frames in the page, and there is no ID available, in this case, we just don't know from which iframe required element is being loaded (It is the case when we do not know the index of the frame also).
The solution for the above concern is, we must find the index of the iframe through which the element is being loaded and then we need to switch to the iframe through the index.
Below are the steps for finding the index of the Frame by which the element is being loaded by using below snippet
Step 1)
WebDriver driver = new FirefoxDriver(); driver.get("http://demo.guru99.com/test/guru99home/"); driver.manage().window().maximize();
Step 2)
int size = driver.findElements(By.tagName("iframe")).size();
Step 3)
Objective for this step would be finding out the index of iframe.
for(int i=0; i<=size; i++){ driver.switchTo().frame(i); int total=driver.findElements(By.xpath("html/body/a/img")).size(); System.out.println(total); driver.switchTo().defaultContent();}
Above "forloop" iterates all the iframes in the page and it prints '1' if our required iframe was found else returns '0'.
Here is the complete code till step 3:
public class IndexOfIframe { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://demo.guru99.com/test/guru99home/"); driver.manage().window().maximize(); //driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); int size = driver.findElements(By.tagName("iframe")).size(); for(int i=0; i<=size; i++){ driver.switchTo().frame(i); int total=driver.findElements(By.xpath("html/body/a/img")).size(); System.out.println(total); driver.switchTo().defaultContent();}}}
Execute this program and output would be like below:
Output:
1 0 0 0 0 0Verify the output, you can find the series of 0's and 1's.
driver.switchTo().frame(0);
driver.findElement(By.xpath("html/body/a/img")).click();
public class SwitchToframe { public static void main(String[] args) throws NoSuchElementException{ WebDriver driver = new FirefoxDriver(); driver.get("http://demo.guru99.com/test/guru99home/"); driver.manage().window().maximize(); //int size = driver.findElements(By.tagName("iframe")).size(); /*for(int i=0; i<=size; i++){ driver.switchTo().frame(i); int total=driver.findElements(By.xpath("html/body/a/img")).size(); System.out.println(total); driver.switchTo().defaultContent(); //switching back from the iframe }*/ //Commented the code for finding the index of the element driver.switchTo().frame(0); //Switching to the frame System.out.println("********We are switched to the iframe*******"); driver.findElement(By.xpath("html/body/a/img")).click(); //Clicking the element in line with Advertisement System.out.println("*********We are done***************"); } }Output: Browser navigates to the page consisting the above iframe and clicks on the iframe.
The Html code for the above nested frame is as shown below.
The above HTML code clearly explains the iframe tag (highlighted in green) within another iframe tag, indicating presence of nested iframes.
Below are the steps for switching to outer frame and printing the text on outer frames: Step 1)
WebDriver driver=new FirefoxDriver(); driver.get("Url"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); int size = driver.findElements(By.tagName("iframe")).size(); System.out.println("Total Frames --" + size); // prints the total number of frames driver.switchTo().frame(0); // Switching the Outer Frame System.out.println (driver.findElement(By.xpath("xpath of the outer element ")).getText());
Once we switch to the outer frame, we should know whether any inner frame present inside the outer frame
Step 2)
size = driver.findElements(By.tagName("iframe")).size(); // prints the total number of frames inside outer frame System.out.println("Total Frames --" + size);
driver.switchTo().frame(0); // Switching to innerframe System.out.println(driver.findElement(By.xpath("xpath of the inner element ")).getText());
public class FramesInsideFrames { public static void main(String[] args) { WebDriver driver=new FirefoxDriver(); driver.get("Url"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); int size = driver.findElements(By.tagName("iframe")).size(); System.out.println("Total Frames --" + size); // prints the total number of frames driver.switchTo().frame(0); // Switching the Outer Frame System.out.println (driver.findElement(By.xpath("xpath of the outer element ")).getText()); //Printing the text in outer frame size = driver.findElements(By.tagName("iframe")).size(); // prints the total number of frames inside outer frame System.out.println("Total Frames --" + size); driver.switchTo().frame(0); // Switching to innerframe System.out.println(driver.findElement(By.xpath("xpath of the inner element ")).getText()); //Printing the text in inner frame driver.switchTo().defaultContent(); } }Output: The output of the above code would print the text in the Inner frame and Outer frame.
Following is a step by step guide to install TestNG in Eclipse Installing TestNG in Eclipse Step 1)...
Selenium Overview: Selenium is an open-source, web Automation Testing tool that supports multiple...
What is Chrome Options class? Chrome options class is used to manipulate various properties of...
Training Summary Selenium is a popular open-source web-based automation tool. This online course is...
What is Sikuli? SIKULI is an open-source GUI based test automation tool. It is mainly used for...
Report generation is very important when you are doing the Automation Testing as well as for...