Frames and Windows

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Frames and windows :

Single frame
:
package Frames;

import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;

public class Frame1 {

public static void main(String[]


args) {
// TODO Auto-generated method
stub

System.setProperty("webdriver.chrome.
driver","D://chromedriver.exe");
ChromeDriver d=new
ChromeDriver();
d.get("https://demoqa.com/frames");
d.manage().window().maximize();

System.out.println(d.getTitle());
System.out.println(
d.getCurrentUrl());

d.switchTo().frame("frame1");
WebElement
text=d.findElement(By.id("sampleHeadi
ng"));
System.out.println(text.getText());

Example2 for single frame :


package Frames;
import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;

public class Single {

public static void main(String[]


args) {
// TODO Auto-generated method
stub
System.setProperty("webdriver.chrome.
driver","D://chromedriver.exe");
ChromeDriver d=new ChromeDriver();
d.get("https://www.dezlearn.com/
iframe-example/");
d.manage().window().maximize();

System.out.println(d.getTitle());
System.out.println(d.getCurrentUrl())
;

d.switchTo().frame("iframe1");
d.findElement(By.xpath("//
button[@id='u_5_6']")).click();

WebElement
text=d.findElement(By.id("processing"
));
System.out.println(text.getText());

Multiple frames:

package Frames;

import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;

public class Nested_Frames {

public static void main(String[]


args) throws InterruptedException {
// TODO Auto-generated method
stub

System.setProperty("webdriver.chrome.
driver","D://chromedriver.exe");
ChromeDriver d=new
ChromeDriver();

d.get("https://www.dezlearn.com/neste
d-iframes-example/");
d.manage().window().maximize();

System.out.println(d.getTitle());

System.out.println(d.getCurrentUrl())
;
System.out.println("First Frame");

d.switchTo().frame("parent_iframe");

d.findElement(By.id("u_5_5")).click()
;
WebElement
t1=d.findElement(By.id("processing"))
;
System.out.println(t1.getText());

Thread.sleep(2000);
System.out.println("second Frame");

d.switchTo().frame("iframe1");

d.findElement(By.id("u_5_6")).click()
;
WebElement
t2=d.findElement(By.id("processing"))
;
System.out.println(t2.getText());
System.out.println("switching to
frist frames");

d.switchTo().defaultContent();

d.switchTo().frame("parent_iframe");
WebElement
t3=d.findElement(By.id("processing"))
;
System.out.println(t3.getText());

Working on multiple windows


We can switch from window to windows
 Get Windowhandle “ get the current window id
Syntax:

Obj.getwindowHandle();//parent id
 Get Windowhandles : get the multiple window ids

Obj.getwindowhandles();//ids of parent
as well as child ids”

If parent id <>child id()


: switch to child id and perform the
operations
// Close the child id
And switch to parent id

MULTIPLE TABS:

package Windows;

import java.util.Set;

import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;
public class New_Tab {

public static void main(String[]


args) {
// TODO Auto-generated method
stub
String
baseurl="https://demoqa.com/browser-
windows";

System.setProperty("webdriver.chrome.
driver","D://chromedriver.exe");
ChromeDriver d=new ChromeDriver();
d.get(baseurl);
d.manage().window().maximize();

String p1=d.getWindowHandle();
System.out.println(p1);//parent id

d.findElement(By.id("tabButton")).cli
ck();

Set<String>p2=d.getWindowHandles();
System.out.println(p2);//parent id as
well as child id
System.out.println(p2.size());

for(String tab1: p2)


{
if(!p1.equals(tab1))
{
d.switchTo().window(tab1);//child
id
WebElement
text=d.findElement(By.id("sampleHeadi
ng"));

System.out.println(text.getText());
}
d.switchTo().window(p1);

}
Multiple windows :
package Windows;
import java.util.Set;

import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;

public class New_Tab {

public static void main(String[]


args) {
// TODO Auto-generated method
stub
String
baseurl="https://demoqa.com/browser-
windows";

System.setProperty("webdriver.chrome.
driver","D://chromedriver.exe");
ChromeDriver d=new ChromeDriver();
d.get(baseurl);
d.manage().window().maximize();
String p1=d.getWindowHandle();
System.out.println(p1);//parent id

d.findElement(By.id("tabButton")).cli
ck();

Set<String>p2=d.getWindowHandles();
System.out.println(p2);//parent id as
well as child id

System.out.println(p2.size());

for(String tab1: p2)


{
if(!p1.equals(tab1))
{
d.switchTo().window(tab1);//child
id
WebElement
text=d.findElement(By.id("sampleHeadi
ng"));

System.out.println(text.getText());
d.close();
}
}

You might also like