There are two ways to find total number of iFrames in a web page. First by executing a JavaScript and second is by finding total number of web elements with a tag name of iFrame. Here is the code using both these methods:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//By executing a java script
JavascriptExecutor exe = (JavascriptExecutor) driver;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);
//By finding all the web elements using iframe tag
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
System.out.println("The total number of iframes are " + iframeElements.size());
to switch to 0th iframe we can simple write driver.switchTo().frame(0). Here is the sample code:
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//Switch by Index
driver.switchTo().frame(0);
driver.quit();
}
Switch to Frames by Name
Now if you take a look at the HTMLcode of iFrame you will find that it has Name attribute. Name attribute has a value iframe1. We can switch to the iFrame using the name by using the command driver.switchTo().frame(“iframe1?). Here is the sample code
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//Switch by frame name
driver.switchTo().frame("iframe1");
driver.quit();
Switch to Frame by ID
Similar to the name attribute in the iFrame tag we also have the ID attribute. We can use that also to switch to the frame. All we have to do is pass the id to the switchTo command like this driver.SwitchTo().frame(“IF1?). Here is the sample code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//Switch by frame ID
driver.switchTo().frame("IF1");
driver.quit();
Switch to Frame by WebElement
Now we can switch to an iFrame by simply passing the iFrame WebElement to the driver.switchTo().frame() command. First find the iFrame element using any of the locator strategies and then passing it to switchTo command. Here is the sample code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//First find the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id("IF1"));
//now use the switch command
driver.switchTo().frame(iframeElement);
driver.quit();
Switching back to Main page from Frame
There is one very important command that will help us to get back to the main page. Main page is the page in which two iFrames are embedded. Once you are done with all the task in a particular iFrame you can switch back to the main page using the switchTo().defaultContent(). Here is the sample code which switches the driver back to main page.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//First find the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id("IF1"));
//now use the switch command
driver.switchTo().frame(0);
//Do all the required tasks in the frame 0
//Switch back to the main window
driver.switchTo().defaultContent();
driver.quit();
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//By executing a java script
JavascriptExecutor exe = (JavascriptExecutor) driver;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);
//By finding all the web elements using iframe tag
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
System.out.println("The total number of iframes are " + iframeElements.size());
to switch to 0th iframe we can simple write driver.switchTo().frame(0). Here is the sample code:
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//Switch by Index
driver.switchTo().frame(0);
driver.quit();
}
Switch to Frames by Name
Now if you take a look at the HTMLcode of iFrame you will find that it has Name attribute. Name attribute has a value iframe1. We can switch to the iFrame using the name by using the command driver.switchTo().frame(“iframe1?). Here is the sample code
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//Switch by frame name
driver.switchTo().frame("iframe1");
driver.quit();
Switch to Frame by ID
Similar to the name attribute in the iFrame tag we also have the ID attribute. We can use that also to switch to the frame. All we have to do is pass the id to the switchTo command like this driver.SwitchTo().frame(“IF1?). Here is the sample code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//Switch by frame ID
driver.switchTo().frame("IF1");
driver.quit();
Switch to Frame by WebElement
Now we can switch to an iFrame by simply passing the iFrame WebElement to the driver.switchTo().frame() command. First find the iFrame element using any of the locator strategies and then passing it to switchTo command. Here is the sample code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//First find the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id("IF1"));
//now use the switch command
driver.switchTo().frame(iframeElement);
driver.quit();
Switching back to Main page from Frame
There is one very important command that will help us to get back to the main page. Main page is the page in which two iFrames are embedded. Once you are done with all the task in a particular iFrame you can switch back to the main page using the switchTo().defaultContent(). Here is the sample code which switches the driver back to main page.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/iframe-practice-page/");
//First find the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id("IF1"));
//now use the switch command
driver.switchTo().frame(0);
//Do all the required tasks in the frame 0
//Switch back to the main window
driver.switchTo().defaultContent();
driver.quit();
No comments:
Post a Comment