Handling Alerts with Selenium

 

// How to handle alerts in Selenium

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HandlingAlerts {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		String Url = "http://sislands.com/coin70/week1/dialogbox.htm";
		driver.get(Url);
		WebElement alert = driver.findElement(By.xpath("html/body/div[1]/center/table/tbody/tr/td/form[1]/p/input"));
		alert.click();
		Alert popup = driver.switchTo().alert();
		System.out.println(popup.getText());
popup.accept();
		Thread.sleep(3000);
		
		driver.close();

		
		
	}
}
  

Get All Links Of Website Home Page

 

// Get All Links Of Website Home Page

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Links {
	
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("https://www.google.co.in/");
		List links = driver.findElements(By.tagName("a"));
		System.out.println(links.size());
		for (int i = 1; i<=links.size(); i=i+1)
		{
			System.out.println(links.get(i).getText());
		}
		
	}

}

Automate Drag and Drop Functionality using Selenium 2.0

 

// How to automate Drag and Drop Functionality using Selenium

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class DragNDrop {

 public static void main(String[] args) throws InterruptedException {
  
  WebDriver driver = new FirefoxDriver();
  String URL = "http://www.dhtmlx.com/docs/products/dhtmlxTree/samples/05_drag_n_drop/06_pro_drag_frame.html";
  driver.get(URL);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
  WebElement from = driver.findElement(By.xpath("//*[@id='treeboxbox_tree']/div/table/tbody/tr[2]/td[2]/table/tbody/tr[4]/td[2]/table/tbody/tr[2]/td[2]/table/tbody/tr/td[4]/span"));
  
  WebElement to = driver.findElement(By.xpath("//*[@id='treeboxbox_tree']/div/table/tbody/tr[2]/td[2]/table/tbody/tr[6]/td[2]/table/tbody/tr[1]/td[4]/span"));
  
  Actions action = new Actions(driver);
  action.dragAndDrop(from, to).perform();
  Thread.sleep(10000);
  driver.close();
  
 }
 
}
  

How to Automate CheckBox/RadioButton Using Selenium WebDriver

 

// How to automate CheckBox/RadioButton

 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class CheckBox {  
      public static void main(String[] args) throws InterruptedException {  
           WebDriver driver = new FirefoxDriver();  
           String Url = "https://www.facebook.com/";  
           driver.get(Url);  
           Boolean select = driver.findElement(By.xpath("//*[@id='persist_box']")).isSelected();  
           if (select == false)  
           {  
                driver.findElement(By.xpath("//*[@id='persist_box']")).click();  
           }  
           Thread.sleep(5000);  
 driver.close();            
      }  
 }  

How to Set up a Project in Eclipse?




¨> Open eclipse (To download eclipse 64 bit or 32 bit, click here)
¨> Right click on Package Explorer > New > Java Project
¨> Give a Project name and click Next
¨> Go to Selenium downloads page and download Java Language bindings (Or Click Here to download Selenium latest jars)