Tuesday, 15 April 2025

Selenium WebDriver Basic Methods You Should Know

 

🔍 Selenium WebDriver Basic Methods You Should Know

Now that your setup is ready, let’s explore some basic Selenium WebDriver methods in Java that you’ll use all the time. These methods help you interact with web elements, navigate between pages, and control the browser like a pro.

🚗 1. get(String url)

Opens a web page in the browser.

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

📄 2. getTitle()

Returns the title of the current web page.

String title = driver.getTitle();
System.out.println("Page title: " + title);

🔗 3. getCurrentUrl()

Gets the current page URL.

String currentUrl = driver.getCurrentUrl();

🧱 4. findElement(By locator)

Finds a single web element using a locator (like ID, name, XPath, etc.).

WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Selenium WebDriver");

🧮 5. findElements(By locator)

Finds multiple elements that match the locator and returns them as a list.

List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total links: " + links.size());

🎯 6. click()

Clicks on a button, link, checkbox, etc.

driver.findElement(By.id("submitBtn")).click();

🖊 7. sendKeys(String text)

Enters text into an input field or textarea.

driver.findElement(By.name("username")).sendKeys("myUser123");

↩ 8. navigate().back(), navigate().forward(), navigate().refresh()

Used for browser navigation, like the back and forward buttons.

driver.navigate().back();    // Go back to the previous page
driver.navigate().forward(); // Move forward in browser history
driver.navigate().refresh(); // Refresh the current page

📷 9. getPageSource()

Returns the full HTML source of the current page.

String source = driver.getPageSource();

🛑 10. quit() vs. close()

  • quit() closes all browser windows and ends the WebDriver session.

  • close() closes only the current browser window.

driver.close(); // Just the active tab
driver.quit();  // Entire browser session

🧠 Quick Tip: Locators You’ll Use Often

By.id("id")
By.name("name")
By.className("className")
By.tagName("tagName")
By.linkText("full link text")
By.partialLinkText("partial text")
By.cssSelector("css selector")
By.xpath("xpath expression")

✅ Practice Makes Perfect

Using these basic methods, you can:

  • Open a browser

  • Search on Google

  • Click links

  • Fill login forms

  • Navigate back and forth

I

How to Install Selenium WebDriver with Java – Step-by-Step Guide


🛠 How to Install Selenium WebDriver with Java – Step-by-Step Guide

Before you can start writing Selenium scripts in Java, you need to set up your system with the right tools. Don’t worry—I'll guide you through it like a friend helping you install a game 😄

✅ Step 1: Install Java (JDK)

Selenium uses Java to run your automation scripts, so you’ll need the Java Development Kit (JDK).

🔗 Download Java JDK

  • Download the latest version (LTS recommended).

  • Install it and set the environment variable JAVA_HOME if it doesn’t happen automatically.

  • Verify by running this in your terminal/command prompt:

    java -version
    

✅ Step 2: Install an IDE (Eclipse or IntelliJ)

You’ll need an IDE (Integrated Development Environment) to write and run your Java code easily.


✅ Step 3: Download the Selenium WebDriver Java Library

You have two easy ways to include Selenium in your project:

💡 Option 1: Using Maven (Recommended)

If you're using Maven (common in most Java projects), just add this to your pom.xml:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.20.0</version>
    </dependency>
</dependencies>

Maven will download everything for you!

💡 Option 2: Manual Download (If you're not using Maven)


✅ Step 4: Download ChromeDriver

Selenium needs a driver to control the browser. For Chrome, you need ChromeDriver.

  • Check your Chrome version (go to Chrome → Help → About Google Chrome).

  • Download the matching ChromeDriver from:
    🔗 https://sites.google.com/chromium.org/driver/

  • Extract it and place it somewhere on your system (like C:/webdriver/ or /usr/local/bin/).

  • In your Java code, point to it like this:

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

✅ Step 5: You're Ready!

Now open your IDE, create a new Java project, and try this quick test script:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");

        System.out.println("Title: " + driver.getTitle());

        driver.quit();
    }
}

If the browser opens and you see Google, congratulations—you’ve successfully set up Selenium with Java!


🔄 Summary

Tool Why You Need It
Java JDK To write and run Java code
Eclipse/IntelliJ To develop in Java with ease
Selenium Java Library To control the browser
ChromeDriver To connect Selenium with Chrome browser


Excel Data read by Selenium java

 Code to read data from Excel  package mit; import java.io.File; import java.io.FileInputStream; import java.util.Arrays; import java.util.I...