MainMenu

Home Java Overview Maven Tutorials

Friday 2 February 2018

How to Handle Frames in Selenium




Hell Friends,

In this article i will describe how to handle the frame , we will get the answers of below questions :

For Video :-

Click Here





1). How to switch to Frame
2). How to handle alert/confirmation on frame.
3). How to switch between the frames.
4). How to come back from frame to main window.

I have created a Web page , which have two frames & in first frame there is some alerts/confirmation etc as shown below :-
Way2testing_title

Testing_page

Name:

datastop

Scenarios are as :


Navigate to WebPage
Enter the data in text box
Switch to frame 1
Click on alert
Accept the alert
Switch to frame 3
click on webelment
Switch to frame 2.
click on element

Here is sample code
import java.io.IOException;
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;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Framedemo
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException, IOException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("file:///C:/Users/adhiraj/Desktop/test.html");
}
@Test(priority = 0)
public void switchframe() throws InterruptedException
{
driver.findElement(By.xpath("html/body/input")).sendKeys("welcome to way2testing");
List framecount = driver.findElements(By.tagName("iframe"));
System.out.println("Total frames on page are : " + framecount.size());
driver.switchTo().frame(0);

driver.findElement(By.xpath(".//*[@id='post-body-2064404811754288590']/form/input[9]")).click();
Thread.sleep(3000);
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath(".//*[@title = 'ifsccode']")));
driver.findElement(By.xpath(".//*[@id='form1']/div[4]/div/div[2]/div[2]/div[2]/table/tbody/tr[1]/td/div/div/button")).click();
driver.switchTo().defaultContent();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.switchTo().frame("way2shayari");
driver.findElement(By.xpath(".//*[@id='post-body-6689423001327694662']/div[1]/table/tbody/tr[1]/td[1]/div/a/img")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
}

Frame Pages for Practice

No comments:

Post a Comment