Selenium Webdriver To Count Number of Elements - Sneha Kesarwani
Selenium Webdriver To Count Number of Elements - Sneha Kesarwani
Seat No : 31010922037
Problem Statement: Write and test a program using Selenium WebDriver to count the total
number of objects (elements) present on a webpage
2. Install ChromeDriver:
● Download the ChromeDriver version that matches your installed Chrome browser
version:
Download ChromeDriver
● 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).
● 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.
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 {
driver.get("https://google.com");
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
Output:
Practical No:
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
2. Install ChromeDriver:
● 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).
● 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.
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");
Thread.sleep(5000); }
catch (Exception e) { e.printStackTrace();
} finally {
// Close the browser after the test is completed
driver.quit();
}
}
}
Output:
Practical No:
2. Install ChromeDriver:
● 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).
● 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.
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");
driver.get("file:///G://ST//number_of_boxes.html");
driver.manage().window().maximize();
int chk = 0;
int unchk = 0;
int rchk = 0;
int runchk = 0;
for(WebElement el : els)
{
if(el.isSelected()) {
chk++;
}
else{
unchk++;
}
}
System.out.println("Total checked items: " +
chk);
System.out.println("Total unchecked items: " +
unchk);
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: