HANDLING WINDOWS/IFRAMES:
In order to handle windows/browsers in selenium, WebDriver API contains following methods which helps the user to switch between windows.
API Url–> https://seleniumhq.github.io/selenium/docs/api/java/
org.openqa.selenium
Interface WebDriver.TargetLocator
defaultContent() Selects either the first frame on the page, or the main document when a page contains iframes. |
|
frame(int index) Select a frame by its (zero-based) index. |
|
frame(java.lang.String nameOrId) Select a frame by its name or ID. |
|
window(java.lang.String nameOrHandle) Switch the focus of future commands for this driver to the window with the given name/handle. |
org.openqa.selenium
Interface WebDriver
java.lang.String |
getWindowHandle() Return an opaque handle to this window that uniquely identifies it within this driver instance. |
java.util.Set<java.lang.String> |
getWindowHandles() Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to switchTo().WebDriver.Options.window() |
Please find the usage of API methods in the following example:
- Access the website URL www.rediff.com
- Enter the Stock Name TCS and click on ‘Get Quote’ button which opens the new Window for displaying the Stock price of TCS.
- Ensure the New window opens displays the Stock price of TCS.
- Execute the Selenium Webdriver Script and retrieve the price and display in the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import java.util.Iterator; import java.util.Set; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * * @author TOTAL-QA * Handling Multiple Browsers/Windows using Selenium WebDriver API * */ public class HandlingWindows { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); ChromeDriver driver = new ChromeDriver();//Opn the browser driver.get("http://www.rediff.com/"); //Switching to a iframe to fetch the details of the price driver.switchTo().frame("moneyiframe"); driver.findElement(By.id("query")).sendKeys("TCS"); driver.findElement(By.cssSelector(".mw001-widget-getquote-btn")).submit(); //Switching to a top window driver.switchTo().defaultContent(); //Confirmation about the focus is using getTitle() method String title = driver.getTitle(); System.out.println("Title::" + title); String url = driver.getCurrentUrl(); System.out.println("url is ::: " + url); //Browsers totally Opened By Webdriver -3 //1. rediff -13 //2. rediffmoney-13 //3. popup-13 //Method fetches the name of the window which has focus String parent = driver.getWindowHandle(); System.out.println("Name of the window:::" + parent); //Set is a DataStructure available in the package java.util //Set allows unique values.It doest not allow duplicates compared to list. //Set cannot be retrieved using index. So,set is not index based like list. //Number of elements in the set can be retrieved using size() method //Method fetches all the names of the window Set<String> wSet= driver.getWindowHandles(); System.out.println("Number of windows:::" + wSet.size()); System.out.println("################Logic 1 - Using Enhanced for loop#################"); for(String x : wSet) { if(!(x.equals(parent))) { driver.switchTo().window(x); System.out.println("Title:::"+ driver.getTitle()); if(driver.getTitle().contains("Tata")) { String price = driver.findElement(By.id("ltpid")).getText(); System.out.println("The price is:::" + price); } } } System.out.println("########################Logic 2 - Using Iterator###################"); Iterator<String> it = wSet.iterator(); while(it.hasNext()) { driver.switchTo().window(it.next()); System.out.println("Title:::"+ driver.getTitle()); if(driver.getTitle().contains("Tata")) { String price = driver.findElement(By.id("ltpid")).getText(); System.out.println("The price is:::" + price); } } System.out.println("###################Logic 3 - Using Single Dimension Array#############"); Object[] wNames = wSet.toArray(); for(int i=0;i<wNames.length;i++) { driver.switchTo().window(wNames[i].toString()); System.out.println("Title:::"+ driver.getTitle()); if(driver.getTitle().contains("Tata")) { String price = driver.findElement(By.id("ltpid")).getText(); System.out.println("The price is:::" + price); } } } } |