We can switch to frame using driver.switchToFrame(); statement. We can use Frame Element or ID or Index to switch. But what if the frame is dynamic? Dynamic in index and dynamic in attributes or if it doesn't have any attributes.
We can use its child element to find the frame. Here I have given a solution to switch to any frame in page using its child element locator. A frame which is having at least one unique element can be switched using this technique.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FindingFrameElementUsingChildElement {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "F:\\SeleniumSoftware\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Maximize browser
driver.manage().window().maximize();
// setup page load timeout
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
// element wait time
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// open url
driver.get("file:///C:/Users/envy/Desktop/FindFrame/Index.html");
//specify child element locator
By childElmLocator = By.id("demotextfrm");
//If child elment found this will switch to that frame and enters text
if(switchToFrameByUsingChild(driver, childElmLocator)) {
driver.findElement(childElmLocator).sendKeys("test");
System.out.println("Switched to Frame where child exist");
}else {
System.out.println("Frame not found.");
}
}
public static boolean switchToFrameByUsingChild(WebDriver driver, By childElmLocator) {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//get all frame element from the page
List frameElems = getAllFrames(driver);
//use forloop to navigate switch one by one frame
for (int frmIndex = 0; frmIndex < frameElems.size(); frmIndex++) {
//switch to frame
driver.switchTo().frame(frameElems.get(frmIndex));
//verify element existance in frame
if (driver.findElements(childElmLocator).size() > 0) {
//if exist exit from method
return true;
//if not exist verify that any frames exist
} else if (getAllFrames(driver).size() > 0) {
//call the switchToChildFrameByChild method
//this verify element existance in child frames
if(switchToChildFrameByChild(driver, childElmLocator)) {
//if exist in child element exit from method
return true;
}else {
//switch to main page if element not exist in frame and child frames
driver.switchTo().defaultContent();
}
}else {
//switch to main page if element not exist in frame
driver.switchTo().defaultContent();
}
}
return false;
}
private static List getAllFrames(WebDriver driver) {
//to get all frame elements that are created using frame and iframe tags
List iFrmLst = driver.findElements(By.tagName("iframe"));
List frmLst = driver.findElements(By.tagName("frame"));
List totalFrms = new ArrayList();
totalFrms.addAll(iFrmLst);
totalFrms.addAll(frmLst);
return totalFrms;
}
private static boolean switchToChildFrameByChild(WebDriver driver, By childElmLocator) {
//get all frames
List cFrameElems = getAllFrames(driver);
int cFrmIndex;
//use for loop to switch one by one frame
for (cFrmIndex = 0; cFrmIndex < cFrameElems.size(); cFrmIndex++) {
//switch to frame
driver.switchTo().frame(cFrameElems.get(cFrmIndex));
//verify element existance
if (driver.findElements(childElmLocator).size() > 0) {
//if exist exit from method
return true;
//if not exist verify for other frames
} else if (getAllFrames(driver).size() > 0) {
//start verifying in other frames by calling same method
if (switchToChildFrameByChild(driver, childElmLocator)) {
//if exist exit from method
return true;
} else {
//switch to parent frame as element not exist
driver.switchTo().parentFrame();
}
}else {
//switch to parent frame as element not exist
driver.switchTo().parentFrame();
}
}
return false;
}
}
No comments :
Post a Comment