Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Creating a basic Selenium script in Java involves several steps, including setting up your development environment, importing the necessary libraries, and writing the script to perform some actions in a web browser. Here’s a step-by-step guide to help you get started:

Step 1: Set Up Your Development Environment

  • Install Java Development Kit (JDK):

  • Install an Integrated Development Environment (IDE):

    • Eclipse or IntelliJ IDEA are popular choices.
  • Add Selenium WebDriver to Your Project:

    • Download the Selenium WebDriver Java client library from the Selenium website.
    • Add the downloaded JAR files to your project’s build path.

Step 2: Write the Selenium Script

Here’s a simple example of a Selenium script in Java that opens a web browser, navigates to a website, and performs some basic actions:

Example Script

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

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

        // Initialize the Chrome driver
        WebDriver driver = new ChromeDriver();

        try {
            // Open a website
            driver.get("https://www.example.com");

            // Print the title of the page
            System.out.println("Page title is: " + driver.getTitle());

            // Find an element by its name attribute and type "Selenium" in it
            WebElement searchBox = driver.findElement(By.name("q"));
            searchBox.sendKeys("Selenium");

            // Submit the search form
            searchBox.submit();

            // Wait for the results to appear (you might want to use WebDriverWait here)
            Thread.sleep(2000);

            // Print the title of the page again
            System.out.println("Page title is: " + driver.getTitle());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

				
			

Step 3: Explanation of the Script

1. Set the Path to the WebDriver Executable:

  • System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
  • Replace "/path/to/chromedriver" with the actual path to your chromedriver executable.

2. Initialize the WebDriver:

  • WebDriver driver = new ChromeDriver();
  • This initializes the Chrome WebDriver. If you are using a different browser, you’ll need to initialize the corresponding WebDriver (e.g., FirefoxDriver for Firefox).

3. Open a Website:

  • driver.get("https://www.example.com");
  • This opens the specified URL in the web browser.

4. Perform Actions on the Web Page:

  • Find elements using ‘driver.findElement(By.name("q"))‘ and perform actions like’ sendKeys‘ and ‘submit‘.

5. Wait for Results:

  • Use ‘Thread.sleep(2000);‘ to wait for 2 seconds. In a real-world scenario, you should use ‘WebDriverWait‘ for better control.

6. Close the Browser:

  • driver.quit();
  • This closes the browser and ends the WebDriver session.

Step 4: Running the Script

1. Compile the Script:

  • In your IDE, compile the script. In Eclipse, you can do this by right-clicking on the project and selecting “Run As” -> “Java Application”.

2. Run the Script:

  • Run the compiled script. The browser should open and perform the actions defined in the script.

This basic script covers the essential steps to start automating web interactions using Selenium in Java. For more advanced interactions and more robust scripts, you may want to explore additional Selenium features such as WebDriverWait, handling different types of web elements, and interacting with JavaScript.

Scroll to Top