Windows:
If
your site opens a new tab or window, Selenium will let you work with it
using a window handle. Each window has a unique identifier which remains
persistent in a single session.
- getWindowhandle(): This method helps to get the window handle of the current window.
- getWindowhandles(): This method helps to get the handles of all the windows opened.
- set: This method helps to set the window handles in the form of a string.
set<string> set= driver.getWindowhandles() - switch to: This method helps to switch between the windows.
Syntax:driver.switchTo().window("windowName");
(OR)
//Store the ID of the original window
String originalWindow = driver.getWindowHandle();
//Click the link which opens in a new window
driver.findElement(By.linkText("new window")).click();
//Wait for the new window or tab
wait.until(numberOfWindowsToBe(2));
//Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if(!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
break;
}
}
String originalWindow = driver.getWindowHandle();
//Click the link which opens in a new window
driver.findElement(By.linkText("new window")).click();
//Wait for the new window or tab
wait.until(numberOfWindowsToBe(2));
//Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if(!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
break;
}
}
/*** To close all the other windows except the main window.** @param openWindowHandle* @return*/public static boolean closeAllOtherWindows(WebDriver driver, String openWindowHandle) {Setfor (String windowHandle : allWindowHandles) {allWindowHandles = driver.getWindowHandles(); if (!windowHandle.equals(openWindowHandle)) {driver.switchTo().window(windowHandle);driver.close();}}driver.switchTo().window(openWindowHandle);if (driver.getWindowHandles().size() == 1)return true;elsereturn false;}
iFrames:
iFrame is basically a tag used in HTML5 like <iframe></iframe>To identify whether an element is on an iframe or not? You just have to right click on the suspected element and check whether you are getting an option such as : ‘This Frame’, ‘View Frame Source’ or ‘Reload Frame’. After right clicking on an element, if you get an option related to frames, that simply means, the element you are trying to locate is aligned on an iframe.To get the count of iframes on a particular web page.int iFrameSize = driver.findElements(By.tagName("iframe")).size();
Switch To iFrame By Index : Switching to iframe using index is probably used when there are multiple iframes present on a single web page. Index of iframe starts with 0 and the index gets increasing with the number of iframes embedded.driver.switchTo().frame(0);driver.switchTo().frame(1);
Switch To iFrame By Name or ID : Name and ID attribute is the most common way of switching to iframe using Selenium.
driver.switchTo().frame("iFrameID");driver.switchTo().frame("frameName");//To move back to the parent framedriver.switchTo().defaultContent();
//T get back to the main (or most parent) framedriver.switchTo().parentFrame();
// to access subframesdriver.switchTo().frame("frameName.0.child");
No comments:
Post a Comment