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

Selenium

The document provides several Java code examples using Selenium WebDriver to automate web browser actions, including opening websites, searching for tutorials, and implementing cross-browser functionality. It demonstrates the use of Chrome and Edge drivers, along with various locators like ID and XPath for interacting with web elements. Each example includes a problem statement, corresponding code, and instructions for execution.

Uploaded by

khadeeja.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Selenium

The document provides several Java code examples using Selenium WebDriver to automate web browser actions, including opening websites, searching for tutorials, and implementing cross-browser functionality. It demonstrates the use of Chrome and Edge drivers, along with various locators like ID and XPath for interacting with web elements. Each example includes a problem statement, corresponding code, and instructions for execution.

Uploaded by

khadeeja.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Problem Statement:

Opening a website on Chrome driver


Solution:
Code :
package BATCH1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.edge.EdgeDriver;
public class DemoBrowser {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​ System.setProperty("webdriver.chrome.driver",
"D:\\Selenium\\chromedriver-win64\\chromedriver.exe");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ wd.get("https://www.somaiya.edu/en/");
​ ​ wd.manage().window().maximize();
​ ​
​ ​ //System.setProperty("webdriver.edge.driver",
"D:\\Selenium\\msedgedriver.exe");
​ ​ //WebDriver wd = new EdgeDriver();
​ ​ //wd.get("https://www.google.co.in/");
​ ​ }
}
Output :
Problem Statement:
Opening a tutorial point on a web driver and search for python tutorial using id.
Code :
package BATCH1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.edge.EdgeDriver;
public class DemoLocator {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​ System.setProperty("webdriver.chrome.driver",
"D:\\Selenium\\chromedriver-win64\\chromedriver.exe");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ wd.manage().window().maximize();
​ ​
wd.get("https://www.tutorialspoint.com/index.htm?gad_source=1&gclid
=Cj0KCQjwhMq-BhCFARIsAGvo0KcC3EHJXoNAjQJCIQX1xU87Hv
Gwz0P72GgJH9LWcwnIdpNbDfqu1uQaAk-dEALw_wcB");
​ ​
wd.findElement(By.id("search-strings")).sendKeys("python");
​ ​ wd.findElement(By.id("search-strings")).sendKeys("\n");
​ ​
​ ​
​ ​ //System.setProperty("webdriver.edge.driver",
"D:\\Selenium\\msedgedriver.exe");
​ ​ //WebDriver wd = new EdgeDriver();
​ ​ //wd.get("https://www.google.co.in/");
​ ​ }
}
Output :
Problem Statement:
Opening a W3school on a web driver and search for python tutorial using xpath.
Code :

package BATCH1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DemoSearch {
​ public static void main(String[] args) throws
InterruptedException {
​ ​ // TODO Auto-generated method stub
​ ​ System.setProperty("webdriver.chrome.driver",
"D:\\Selenium\\chromedriver-win64\\chromedriver.exe");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ wd.get("https://www.w3schools.com/");
​ ​ wd.manage().window().maximize();
​ ​
wd.findElement(By.xpath("/html/body/div[5]/div[1]/div/div/div/div[1]
/div/form/input")).sendKeys("Python tutorial");
​ ​ Thread.sleep(2000);
​ ​
wd.findElement(By.xpath("/html/body/div[5]/div[1]/div/div/div/div[1]
/div/form/button")).click();
​ ​
​ }
}
Output :
Problem Statement:
Cross Browsing using switch case.
Code :
package BATCH1;
import java.util.Scanner;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
public class DemoCrossBrowser {
​ public static void main(String[] args) {
​ ​ Scanner sc = new Scanner(System.in);
​ ​ // TODO Auto-generated method stub
​ ​ System.out.println("1- Chrome");
​ ​ System.out.println("2 - Edge");
​ ​ int i =sc.nextInt();
​ ​ switch(i) {
​ ​ case 1 : ​ ​
​ ​ ​ System.setProperty("webdriver.chrome.driver",
"D:\\Selenium\\chromedriver-win64\\chromedriver.exe");
​ ​ ​ WebDriver wd = new ChromeDriver();
​ ​ ​ wd.get("https://www.somaiya.edu/en/");
​ ​ ​ wd.manage().window().maximize();
​ ​ ​ break;
​ ​
​ ​ case 2 : ​ ​
​ ​ ​ System.setProperty("webdriver.edge.driver",
"D:\\Selenium\\msedgedriver.exe");
​ ​ ​ WebDriver wd1 = new EdgeDriver();
​ ​ ​ wd1.get("https://www.google.co.in/");
​ ​ ​ wd1.manage().window().maximize();
​ ​ ​ break;
​ ​ }
​ ​
​ }
}

Output :
On Choosing option 1 :

On Choosing option 2 :
20/3/25
package batch1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
​ import org.openqa.selenium.chrome.ChromeDriver;
public class demolocator {​
​ ​ public static void main(String[] args) {
​ ​ ​ // TODO Auto-generated method stub
​ ​ ​ System.setProperty("webdriver.chrome.driver",
"D:\\Selenium\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
​ ​ ​ WebDriver wd = new ChromeDriver();
​ ​ ​ wd.get("https://www.tutorialspoint.com/index.htm");
​ ​ ​ wd.findElement(By.id("search-strings")).sendKeys("hello");
​ ​ ​ wd.findElement(By.id("search-strings")).sendKeys("/n");
​ ​ ​ wd.manage().window().maximize();
​ ​ }
​ }

package Batch1;
import java.util.Scanner;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
public class DemoCrossBrowser {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​ Scanner sc = new Scanner(System.in);
​ ​ System.out.println("1: Chrome");
​ ​ System.out.println("2: Edge");
​ ​
​ ​ int ch = sc.nextInt();
​ ​ switch(ch) {
​ ​ case 1:
​ ​ ​
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\chromedriver-win64\\chromedriver-win64\\c
hromedriver.exe");
​ ​ ​ WebDriver wd = new ChromeDriver();
​ ​ ​ wd.get("https://www.w3schools.com/");
​ ​ ​ wd.manage().window().maximize();
​ ​ ​ break;
​ ​ ​
​ ​ case 2:
​ ​ ​
System.setProperty("webdriver.edge.driver","D:\\Selenium\\edgedriver_win64\\edgedriver_win64\\msedge
driver.exe");
​ ​ ​ WebDriver wd2 = new EdgeDriver();
​ ​ ​ wd2.get("https://www.w3schools.com/");
​ ​ ​ wd2.manage().window().maximize();
​ ​ ​ break;
​ ​ default:
​ ​ ​ System.out.println("Invalid Option!");
​ ​ }
​ }
}

package Batch1;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
public class DemoSearch {
​ public static void main(String[] args) {
​ ​ // TODO Auto-generated method stub
​ ​
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\chromedriver-win64\\chromedriver-win64\\c
hromedriver.exe");
​ ​ WebDriver wd = new ChromeDriver();
​ ​ wd.get("https://www.w3schools.com/");
​ ​ wd.manage().window().maximize();
​ ​
wd.findElement(By.xpath("/html/body/div[5]/div[1]/div/div/div/div[1]/div/form/input")).sendKeys("Python
tutorial");
​ ​ wd.findElement(By.xpath("//*[@id=\"learntocode_searchicon\"]")).click();
​ }
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy