Showing posts with label Alerts and Iframes Handle and Capture Text from Webpage. Show all posts
Showing posts with label Alerts and Iframes Handle and Capture Text from Webpage. Show all posts

Wednesday, May 8, 2019



Handle Alerts, Iframes and Capture Text from webpage using Selenium Java

package Package;

//import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

//import java.util.concurrent.TimeUnit;
//import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;


public class AzziAutomation
  {

public static void main (String[] args) throws InterruptedException
  {

System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
 WebDriver driver = new ChromeDriver();   
String url ="http://www.xpr2t.com/Webobjects.html";

// Maximize the browser
driver.manage().window().maximize();

// Open the application in the browser
  driver.get(url);
//  driver.navigate().to(url);   ---- ANOTHER WAY OF PASSING URL
// Hard wait for 5 sec
// Thread.sleep(5000);      
     
     
//  MANAGET ALERT -  CLICK ON ALERT BUTTON
       driver.findElement(By.xpath("html/body/table[9]/tbody/tr/td/button")).click();  
// CAPTURING THE Alert Message            
     
  String alertMessage = driver1.switchTo().alert().getText();
   System.out.println(alertMessage);
//CLICK ON ALERT OK BUTTON
   driver1.switchTo().alert().accept();
//Click ON CANCEL BUTTON
   //driver.switchTo().alert().dismiss();
   Thread.sleep(5000);

// HANDLE TABLE - CAPTURE TEXT
 
 
String text1=  driver.findElement(By.xpath("html/body/table[10]/tbody/tr/td")).getText();
  System.out.println("Text value :"+text1);
   //another way of get text
 
//   WebElement tablevalue = driver.findElement(By.xpath("html/body/table[10]/tbody/tr/td"));
//System.out.println("Another approach cell value :"+tablevalue.getText());
   

   
//CLOSE THE CURRENT BROWSER
       driver.close();
   
   
//CLOSE ALL OPENED BROWSERS
       driver.quit();
 }
  }



//  Handle Iframes in Selenium Java

 3 Different ways to handle Iframes in Selenium Java.

               1.    switchTo().frame(index)
               2.    switchTo().frame(name)
               3.     switchTo().frame(WebElement)




package Package;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
//import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

//https://selenium.googlecode.com/git/docs/api/java/overview-summary.html
public class AzziAutomationProject
  {



public static void main (String[] args) throws InterruptedException
  {


//*************************** CHROME DRIVER *****************************

/  System.setProperty("webdriver.chrome.driver", "C:\\Ruby22-x64\\bin\\chromedriver.exe");
   WebDriver driver = new ChromeDriver();

 
   String url ="http://www.xpr2t.com/Webobjects.html";
// String url ="http://www.xpr2t.com/Webobjects.html";
// Maximize the browser
driver.manage().window().maximize();

//driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
   // Open the application in the browser
   // Open Url
  driver.get(url);
//  driver.navigate().to(url);


  // SWITCH TO  FRAME USING WEBELEMENT -xpath
  WebElement  iframe= driver.findElement(By.tagName("iframe"));

 driver.switchTo().frame(iframe);   

     driver.findElement(By.linkText("REGISTER")).click();
     Thread.sleep(5000);
     driver.switchTo().defaultContent();

  // SWITCH TO  FRAME USING INDEX
 
   driver.switchTo().frame(0);   
 
       driver.findElement(By.linkText("REGISTER")).click();
       Thread.sleep(5000);
       driver.switchTo().defaultContent();

 // SWITCH TO  FRAME USING NAME  - currently http://www.xpr2t.com/Webobjects.html doesn't have the name for the Iframe..
 
  //  driver.switchTo().frame(name );   
 
  //      driver.findElement(By.linkText("REGISTER")).click();
  //      Thread.sleep(5000);
  //      driver.switchTo().defaultContent();

//Get the no of Iframes in the application

  int size = driver.findElements(By.tagName("iframe")).size();
System.out.println(size);
 


       //CLOSE THE BROWSER
       driver.close();
       //CLOSE ALL BROWSERS
       driver.quit();

  }
  }