Selenium P10s
Published: 2022-11-21 15:50:50

                  Practical - 1
Q: Launch any Web browser, Open Website
(https://nuv.ac.in), Print Title, and URL of this Website.

Code:

package practical1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/Users/surajsatheesh/Developer/Selenium
drivers//chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://nuv.ac.in");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
}
}

                Practical – 2
Q: Launch any web browser , use Locators – ID, Name,
className , use Click() method on any button and print
size .

Code:

package Practical2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/Users/surajsatheesh/Developer/Selenium
drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.saucedemo.com/");
driver.findElement(By.id("username")).
sendKeys("standard_user");
driver.findElement(By.name("password")).sendKeys("secret_sauce
");
driver.findElement(By.className("submitbutton")).
click();
driver.findElement(By.linkText("Sauce Labs
Backpack")).click();
driver.findElement(By.partialLinkText("Hello")).click();
List < WebElement> webElementList =
driver.findElements(By.partialLinkText("Sauce"));
System.out.println("Element Sizes : " +
webElementList.size());
}
}

                 Practical – 3
Q: Launch any Web browser, Open Website
(https://www.amazon.in/), Print Title, URL of this
Website , size, use tagname by Anchor tag and print list
of hyperlinks of that page.

Code:

package Hyperlink;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","/Users/
surajsatheesh/Developer/Selenium
drivers//chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.amazon.in/");
List hyperlinks =
driver.findElements(By.tagName("a"));
System.out.println("Total Links" +
hyperlinks.size());
for(WebElement Links: hyperlinks){
System.out.println(Links.getText());
}
}
}

                Practical – 4
Q: Launch any Web browser, Open Website
(https://www.opencart.com/index.php?route=account/re
gister), Print Title, URL of this Website , print size and
Print dropdown list of that Country and use isMultiple()
Method.

Code:
package dropdown;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Main {
public static void main (String[] args){
System.setProperty("webdriver.chrome.driver", "");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.opencart.com/index.php?route=account/r
egister");
WebElement element = driver.findElement(By.id("inputcountry"));
Select dropdown = new Select(element);
dropdown.selectByVisibleText("India");
if(dropdown.isMultiple()){
System.out.println("Dropdown is multiple");
}
else
{
System.out.println("Dropdown is not multiple");
}
}
}

                    Practical – 5
Q: Write the program to count number of radio buttons
on a webpage.
Code:
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/Users/surajsatheesh/Developer/Selenium drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/checkboxradio/");
List<WebElement> radio = driver.findElements(By.tagName("a"));
System.out.println("Number of radio buttons: "+ radio.size());
}
}

                     Practical – 6
Q: Write a program to find total no. of Rows and
Columns in web table using Maven Project.
Code:
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class table {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","/Users/surajsatheesh/Developer
/Selenium drivers//chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("file:///Users/surajsatheesh/Developer/Selenium/Practical%205/index.
html");
String text =
driver.findElement(By.xpath("/html/body/table/tbody/tr[2]/td[1]")).getText();
System.out.println(text);
List < WebElement> rows =
driver.findElements(By.xpath("/html/body/table/tbody/tr"));
System.out.println("Total rows: "+rows.size());
for(WebElement row: rows){
System.out.println(row.getText());
}
}
}

                    Practical – 7
Q: Write a program to open new tab or window in
selenium web driver.
Code:
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/Users/surajsatheesh/Developer/Selenium drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com");
driver.switchTo().newWindow(WindowType.TAB);
driver.switchTo().newWindow(WindowType.WINDOW);
}
}

                   Practical – 8
Q: Write a program to handle Tooltip in selenium web
driver.
Code:
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/Users/surajsatheesh/Developer/Selenium
drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
WebElement link = driver.findElement(By.linkText("Sign Up"));
Actions action = new Actions(driver);
action.moveToElement(link).perform();
String tooltip = link .getAttribute("title");
System.out.println("Tooltip: "+tooltip);
}
}

                 Practical – 9
Q: Write a program to get Image source URL.
Code:
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"/Users/surajsatheesh/Developer/Selenium drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https:\\www.facebook.com");
WebElement image = driver.findElement(By.xpath("//img[@alt='Facebook']"));
System.out.println("Src attribute Value: " + image.getAttribute("src"));
driver.quit();
}
}

                   Practical – 10
Q: Write a program to automate date picker in selenium
web driver.
Code:
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Main {
public static void main(String[] args) {
String expectedDay = "21";
String expectedMonth = "April";
String expectedYear = "2025";
System.setProperty("webdriver.chrome.driver",
"/Users/surajsatheesh/Developer/Selenium drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/datepicker/");
driver.switchTo().frame(0);
WebElement datepicker = driver.findElement(By.id("datepicker"));
datepicker.click();
while(true)
{
String calenderMonth = driver.findElement(By.xpath("//span[@class='uidatepicker-
month']")).getText();
String calenderYear = driver.findElement(By.xpath("//span[@class='ui-datepickeryear']")).
getText();
if(calenderMonth.equals(expectedMonth) && calenderYear.equals(expectedYear))
{
List<WebElement> daysList =
driver.findElements(By.xpath("//table/tbody/tr/td"));
for(WebElement e : daysList)
{
String calenderDay = e.getText();
if(calenderDay.equals(expectedDay))
{
e.click();
break;
}
}
break;
}
else
{
driver.findElement(By.xpath("//span[@class='ui-icon ui-icon-circle-trianglee']")).
click();
}
}
}
}