0% found this document useful (0 votes)
104 views

Selenium Programs

The document contains code examples demonstrating various Selenium WebDriver techniques including: using XPath contains to locate elements, selecting options from dropdowns, getting the current URL and page title, using forward and back navigation, handling alerts, and getting an element's location, size, CSS values and attributes.

Uploaded by

Aishwarya Mittal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Selenium Programs

The document contains code examples demonstrating various Selenium WebDriver techniques including: using XPath contains to locate elements, selecting options from dropdowns, getting the current URL and page title, using forward and back navigation, handling alerts, and getting an element's location, size, CSS values and attributes.

Uploaded by

Aishwarya Mittal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

//xpath using contains

package Pack1;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Class1 {

public static void main(String[] args)

System.setProperty("webdriver.gecko.driver","C:\\Users\\Amit\\Downloads\\Aishwarya\\ge
ckodriver-v0.24.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("https://facebook.com");

driver.findElement(By.id("email")).sendKeys("Fahad");

driver.findElement(By.id("pass")).sendKeys("Fahad");

//driver.findElement(By.xpath("//input[@id,'u_0_')]")).click();

driver.findElement(By.xpath("//input[contains(@id,'u_0_')]")).click(); //using
contains

//dropdown selection

package Pack1;

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.support.ui.Select;
public class Class2 {

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

System.setProperty("webdriver.gecko.driver","C:\\Users\\Amit\\Downloads\\Aishwarya\\ge
ckodriver-v0.24.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("https://facebook.com");

WebElement day1= driver.findElement(By.id("day"));

WebElement month1 = driver.findElement(By.id("month"));

WebElement year1 = driver.findElement(By.id("year"));

Select dayselect = new Select(day1);

dayselect.selectByIndex(10);

Thread.sleep(3000);

Select monthselect = new Select(month1);

monthselect.selectByVisibleText("July");

Thread.sleep(3000);

Select yearselect = new Select(year1);

yearselect.selectByVisibleText("1993");

Thread.sleep(3000);

driver.findElement(By.xpath("//input[@value='1']")).click();

//using of getCurrenturl and getTitle

package Pack1;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Class3 {


public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Users\\Amit\\Downloads\\Aishwarya\\ge
ckodriver-v0.24.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("https://facebook.com");

//driver.findElement(By.xpath("//input[@value='1']")).click();

String s1 = driver.getCurrentUrl(); //getting current url

String s2 = driver.getTitle(); //getting title

System.out.println("the current url is" + s1);

System.out.println("the title is : " + s2);

//use of forward and back

package Pack1;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Class4 {

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Users\\Amit\\Downloads\\Aishwarya\\ge
ckodriver-v0.24.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("https://www.facebook.com");

driver.navigate().to("https://www.gmail.com");

driver.navigate().forward();

driver.navigate().back();
}

//use of alerts

package Pack1;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class AlertClass5 {

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

//alerts - demo.automationtesting.in/Alerts.html

System.setProperty("webdriver.gecko.driver","C:\\Users\\Amit\\Downloads\\Aishwarya\\ge
ckodriver-v0.24.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("http://demo.automationtesting.in/Alerts.html");

//alert1

//driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[1]/ul/li[1]/a")).click();

//driver.findElement(By.xpath("//*[@id='OKTab']/button")).click();

//Thread.sleep(3000);

//driver.switchTo().alert().accept();

//Thread.sleep(4000);

//alert2

//driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[1]/ul/li[2]/a")).click();

// driver.findElement(By.xpath("//*[@id='CancelTab']/button")).click();

//Thread.sleep(5000);
// driver.switchTo().alert().accept(); //accepted

//driver.switchTo().alert().dismiss(); //cancel

//alert3

driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[1]/ul/li[3]/a")).click();

driver.findElement(By.xpath("//*[@id='Textbox']/button")).click();

Thread.sleep(5000);

driver.switchTo().alert().sendKeys("Testing Selenium");;

Thread.sleep(3000);

driver.switchTo().alert().accept();

//driver.switchTo().alert().dismiss(); //cancel

//getting the location,height and width and css value of an element

package Pack1;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Class6 {

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

System.setProperty("webdriver.gecko.driver","C:\\Users\\Amit\\Downloads\\Aishwarya\\ge
ckodriver-v0.24.0-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("https://facebook.com");
//getting the size of the element

WebElement element = driver.findElement(By.id("email"));

System.out.println("the size of the element is : " + element.getSize()); //height and


width

System.out.println("the location of the element is : " + element.getLocation()); //x


axis and y axis

System.out.println("the css value of the element is : " +


element.getCssValue("background-color")); // background color

Thread .sleep(6000);

//google

driver.navigate().to("https://www.google.com");

WebElement element1 = driver.findElement(By.name("q"));

System.out.println("the size of the google element is : " + element1.getSize());


//height and width

System.out.println("the location of the google element is : " +


element1.getLocation()); //x axis and y axis

System.out.println("the css value of the google element is : " +


element1.getCssValue("background-color")); // background color

System.out.println("the font size of the google element is: " +


element1.getCssValue("font-size")); //font size

System.out.println("the value of the the google element is:" +


element1.getAttribute("name")); //value of the attribute

WebElement element2 = driver.findElement(By.name("q")); //entering the text in


the search bar

element2.sendKeys("yahoo");

element2.submit(); //submit the text search

You might also like