0% found this document useful (0 votes)
63 views14 pages

Selenium Webdriver To Count Number of Elements - Sneha Kesarwani

Uploaded by

Sneha Kesarwani
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)
63 views14 pages

Selenium Webdriver To Count Number of Elements - Sneha Kesarwani

Uploaded by

Sneha Kesarwani
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/ 14

Name -Sneha Dilip Kesarwani

Seat No : 31010922037

Subject : Software Testing


Practical No:

Title: Selenium Webdriver

Problem Statement: Write and test a program using Selenium WebDriver to count the total
number of objects (elements) present on a webpage

Step 1: Set Up the Environment 1.

Install Java and Eclipse IDE:

● JDK (Java Development Kit): Download JDK

● Eclipse IDE: Download Eclipse IDE

2. Install ChromeDriver:

● ChromeDriver is needed to control Google Chrome with Selenium.

● Download the ChromeDriver version that matches your installed Chrome browser
version:
Download ChromeDriver

3. Download Selenium WebDriver Libraries:

● Selenium WebDriver: You need to add Selenium libraries to your Java project in
Eclipse.

● Download Selenium JARs o Download the Selenium Java bindings & Selenium

Server
o Extract the ZIP file and note the location of the .jar files (both the main JAR and
the JARs in the "libs" folder).

4. Add JARs to Eclipse Project:

● Open Eclipse IDE and create a new Java Project.

● Right-click on the project in the Package Explorer and select Properties.

● Go to Java Build Path > Libraries > Add External JARs.

● Add the downloaded Selenium .jar files (both the main selenium-server.jar and all .jar
files from the "libs" folder).

● Also, add the ChromeDriver executable to your system's PATH, or you can specify its
location in your code.

Step 2: Selenium WebDriver Code to count number of elements on the webpage

Java Code:
package web_driver;
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
count_elements
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
"C://STpracs//chromedriver-win64//chromedriver-win64//chromedriver.exe");
// Initialize ChromeDriver
WebDriver driver = new ChromeDriver();
try {

// Navigate to the login page (Example URL)

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

// Maximize the browser window

driver.manage().window().maximize();
// Get a list of all web elements (objects) on the webpage
List<WebElement>allElements =driver.findElements(By.xpath("//*"));
// Print the total number of elements found

System.out.println("Total number of elements on the webpage:" +


allElements.size());
Thread.sleep(5000);
}
catch (Exception e) { e.printStackTrace();
}
finally {

// Close the browser after the test is completed


driver.quit();
}
}

Output:
Practical No:

Title: Selenium Webdriver

Problem Statement: Write and test a program using Selenium WebDriver to count the total
number items in a list/combo. Step 1: Set Up the Environment

1. Install Java and Eclipse IDE:

● JDK (Java Development Kit): Download JDK

● Eclipse IDE: Download Eclipse IDE

2. Install ChromeDriver:

● ChromeDriver is needed to control Google Chrome with Selenium.


● Download the ChromeDriver version that matches your installed Chrome browser
version:
Download ChromeDriver

3. Download Selenium WebDriver Libraries:


● Selenium WebDriver: You need to add Selenium libraries to your Java project in
Eclipse.

● Download Selenium JARs o Download the Selenium Java bindings & Selenium

Server

o Extract the ZIP file and note the location of the .jar files (both the main JAR and
the JARs in the "libs" folder).

4. Add JARs to Eclipse Project:

● Open Eclipse IDE and create a new Java Project.

● Right-click on the project in the Package Explorer and select Properties.

● Go to Java Build Path > Libraries > Add External JARs.

● Add the downloaded Selenium .jar files (both the main selenium-server.jar and all .jar
files from the "libs" folder).

● Also, add the ChromeDriver executable to your system's PATH, or you can specify its
location in your code.

Step 2: Selenium WebDriver Code to count number of elements on the webpage

Java Code:
package web_driver;
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; import
java.util.List; public class count_box
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
"C://STpracs//chromedriver-win64//chromedriver-win64//chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
// Navigate to https://www.facebook.com/r.php
driver.get("https://www.facebook.com/r.php");

// Maximize the browser window


driver.manage().window().maximize();
Select se = new
Select(driver.findElement(By.xpath("//Select[@id='day']")));

Select se1 = new


Select(driver.findElement(By.xpath("//Select[@id='month']")));

Select se2 = new


Select(driver.findElement(By.xpath("//Select[@id='year']")));

List <WebElement> mylist = se.getOptions();


mylist.size();
System.out.println("Number of items in day list: "
+mylist.size());

List <WebElement> mylist1 = se1.getOptions();


mylist1.size();
System.out.println("Number of items in month list: "
+mylist1.size());

List <WebElement> mylist2 = se2.getOptions();


mylist2.size();
System.out.println("Number of items years list: " +mylist2.size());

Thread.sleep(5000); }
catch (Exception e) { e.printStackTrace();
} finally {
// Close the browser after the test is completed
driver.quit();
}
}

}
Output:

Practical No:

Title: Selenium Webdriver


Problem Statement: Write and test a program using Selenium WebDriver to count the
number of checked and unchecked checkboxes/ radio buttons on the page

Step 1: Set Up the Environment 1.

Install Java and Eclipse IDE:

● JDK (Java Development Kit): Download JDK

● Eclipse IDE: Download Eclipse IDE

2. Install ChromeDriver:

● ChromeDriver is needed to control Google Chrome with Selenium.


● Download the ChromeDriver version that matches your installed Chrome browser
version:
Download ChromeDriver
3. Download Selenium WebDriver Libraries:

● Selenium WebDriver: You need to add Selenium libraries to your Java project in
Eclipse.

● Download Selenium JARs o Download the Selenium Java bindings & Selenium

Server

o Extract the ZIP file and note the location of the .jar files (both the main JAR and
the JARs in the "libs" folder).

4. Add JARs to Eclipse Project:

● Open Eclipse IDE and create a new Java Project.

● Right-click on the project in the Package Explorer and select Properties.

● Go to Java Build Path > Libraries > Add External JARs.

● Add the downloaded Selenium .jar files (both the main selenium-server.jar and all .jar
files from the "libs" folder).

● Also, add the ChromeDriver executable to your system's PATH, or you can specify its
location in your code.

Step 2: Selenium WebDriver Code to count number of elements on the webpage

HTML Code:

<html>
<form>
<h2>Select Gender</h2>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other" checked> Other<br>
<h2>Select Languages Known</h2>
<input type="checkbox" name="lang" value="Java" checked="checked">
Java<br>
<input type="checkbox" name="lang" value="PHP" checked="checked">
PHP<br>
<input type="checkbox" name="lang" value="C#"> C#<br>
<input type="checkbox" name="lang" value="Python" checked="checked">
Python<br>
<input type="checkbox" name="lang" value="Ruby"> Ruby<br> <br><br>
<input type="submit" value="Submit">
</form>
</html>
Java Code:

package web_driver;
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; import
java.util.List; public class p6 { public
static void main(String[] args) {
// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver",
"C://STpracs//chromedriver-win64//chromedriver-win64//chromedriver.exe");

WebDriver driver = new ChromeDriver();


try {
// Navigate to the page

driver.get("file:///G://ST//number_of_boxes.html");

// Maximize the browser


window

driver.manage().window().maximize();

int chk = 0;
int unchk = 0;
int rchk = 0;
int runchk = 0;

List <WebElement> els =


driver.findElements(By.xpath("//input[@type='checkbox']"));

for(WebElement el : els)
{

if(el.isSelected()) {
chk++;
}
else{

unchk++;
}
}
System.out.println("Total checked items: " +
chk);
System.out.println("Total unchecked items: " +
unchk);

List <WebElement> elm =


driver.findElements(By.xpath("//input[@type='radio']"));

for(WebElement ell : elm)


{
if(ell.isSelected())
{ rchk++;
}
else{

runchk++;
}
}
System.out.println("Total checked radio items: "
+ rchk);
System.out.println("Total unchecked radio items:
" + runchk);

Thread.sleep(5000);
} catch (Exception e)
{ e.printStackTrace();
} finally {
// Close the browser after the test is completed
driver.quit();
}

Output:

You might also like