0
0
Selenium Javatesting~5 mins

Opening URLs (driver.get) in Selenium Java

Choose your learning style9 modes available
Introduction

We open URLs in tests to visit web pages and check if they work correctly.

When you want to start testing a website from its homepage.
When you need to check if a specific page loads without errors.
When you want to test navigation by opening different pages.
When you want to verify page content after opening a URL.
When you want to automate filling forms on a web page.
Syntax
Selenium Java
driver.get("https://example.com");
Use double quotes for the URL string.
driver.get() waits until the page is fully loaded before continuing.
Examples
Opens Google's homepage.
Selenium Java
driver.get("https://www.google.com");
Opens the login page of example.com.
Selenium Java
driver.get("https://example.com/login");
Opens the dashboard page after login.
Selenium Java
driver.get("https://mywebsite.com/dashboard");
Sample Program

This test opens https://www.example.com, checks if the page title contains 'Example', prints the result, and closes the browser.

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

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

        WebDriver driver = new ChromeDriver();

        // Open the URL
        driver.get("https://www.example.com");

        // Check page title contains 'Example'
        String title = driver.getTitle();
        if (title.contains("Example")) {
            System.out.println("Test Passed: Page loaded with title: " + title);
        } else {
            System.out.println("Test Failed: Unexpected page title: " + title);
        }

        // Close the browser
        driver.quit();
    }
}
OutputSuccess
Important Notes

Make sure the browser driver (like chromedriver) is installed and its path is set correctly.

driver.get() waits for the page to load before moving on, so you don't need extra waits for page load.

Always close the browser with driver.quit() to free resources.

Summary

Use driver.get() to open a web page in your test.

It waits until the page fully loads before continuing.

Check page content or title after opening to verify the page loaded correctly.