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

To get the current URL and title of a webpage using Selenium in Java, you need to follow these steps:

  1. Set up your Selenium environment: Ensure you have the necessary dependencies for Selenium. If you are using Maven, add the Selenium dependency to your pom.xml.

  2. Initialize the WebDriver: Create an instance of the WebDriver. For this example, we’ll use ChromeDriver.

  3. Navigate to a webpage: Use the WebDriver to open a webpage.

  4. Retrieve the current URL and title: Use the appropriate WebDriver methods to get the URL and title of the current page.

Here’s a detailed step-by-step example:

Step 1: Set up your Selenium environment

If you are using Maven, add the following dependency to your ‘pom.xml‘ file:

				
					<dependencies>
    <!-- Selenium Java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0</version>
    </dependency>
    
    <!-- Selenium Chrome Driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>4.0.0</version>
    </dependency>
</dependencies>

				
			

Step 2: Initialize the WebDriver

You need to have ChromeDriver executable in your system. Download it from ChromeDriver download page and place it in a suitable location.

Step 3: Write your Java code

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

public class GetCurrentURLAndTitle {

    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        
        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        
        // Navigate to a webpage
        driver.get("https://www.example.com");
        
        // Get the current URL
        String currentURL = driver.getCurrentUrl();
        System.out.println("Current URL: " + currentURL);
        
        // Get the page title
        String pageTitle = driver.getTitle();
        System.out.println("Page Title: " + pageTitle);
        
        // Close the browser
        driver.quit();
    }
}

				
			

Detailed Explanation

1. Setting up ChromeDriver:

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

				
			

This line sets the system property for ChromeDriver. Replace ‘"path/to/chromedriver"‘ with the actual path to the ChromeDriver executable on your machine.

2. Initializing WebDriver:

				
					WebDriver driver = new ChromeDriver();

				
			

This initializes a new instance of ChromeDriver, which will open a new Chrome browser window.

3. Navigating to a webpage:

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

				
			

This command tells the browser to open the specified URL.

4. Getting the current URL:

				
					String currentURL = driver.getCurrentUrl();
System.out.println("Current URL: " + currentURL);

				
			

This retrieves the current URL of the webpage and prints it to the console.

5. Getting the page title:

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

				
			

This retrieves the title of the webpage and prints it to the console.

6. Closing the browser:

				
					driver.quit();

				
			

This closes the browser and ends the WebDriver session.

Running the Code

  • Ensure you have the ChromeDriver executable downloaded and its path set correctly in the code.
  • Ensure your Maven project is set up correctly with the required dependencies.
  • Run the GetCurrentURLAndTitle class as a Java application.

This example demonstrates the basics of navigating to a webpage and retrieving its URL and title using Selenium WebDriver in Java.

Scroll to Top