Retrieve Web Page Title with WebDriver in Java

Q

How to Retrieve Web Page Title with Selenium WebDriver in Java?

✍: FYIcenter.com

A

Once you have connected to the Web browser using the Selenium WebDriver, you can open any remote Web page and retrieve its page title as shown in this tutorial.

3 main methods from the Selenium WebDriver class will be used:

  • driver.get() - Loads a new Web page from the given URL in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.
  • dirver.getTitle() - Returns the title of the current Web page, with leading and trailing whitespace stripped, or null if it is not set in the Web page.
  • driver.quit() - Closes all open pages and quits this driver.

1. Make sure you have WebDriver loading program, WebDriverLoader.java, compiled in the current directory:

C:\fyicenter> dir WebDriverLoader.*

             1,693 WebDriverLoader.class
               882 WebDriverLoader.java

2. Enter the following program, GetPageTitle.java, that retrieve Web page title:

C:\fyicenter> type GetPageTitle.java 

// GetPageTitle.java
// Copyright (c) FYIcenter.com 
import org.openqa.selenium.*;
public class GetPageTitle {
   public static void main(String[] args) {
      WebDriver driver = WebDriverLoader.load(args[0]);
      driver.get("http://sqa.fyicenter.com");
      String title = driver.getTitle();
      driver.quit();
   
      System.out.println("Test Case - Verify Page Title:");
      System.out.println("   Actual Value: "+title);
      System.out.println("   Expected Value: FYI Center...");
      if (title.startsWith("FYI Center")) {
         System.out.println("   Test Result: Passed");
      } else {
         System.out.println("   Test Result: Failed");
      }
   }
}

3. Compile and run it with the Selenium Client JAR file. Make sure the current directory is included in the "-classpath".

C:\fyicenter> javac -classpath \
   .;\fyicenter\selenium\java\client-combined-3.141.59.jar \
   GetPageTitle.java

C:\fyicenter> java -classpath \
   .;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
   \fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
   \fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
   \fyicenter\selenium\java\libs\okio-1.14.0.jar;\
   \fyicenter\selenium\java\libs\commons-exec-1.3.jar \
   GetPageTitle Chrome
Starting ChromeDriver 75.0.3770.8 (...-refs/branch-heads/3770@{#40}) on port 47740
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
8:02:26 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Test Case - Verify Page Title:
   Actual Value: FYI Center for Software QA Testing
   Expected Value: FYI Center...
   Test Result: Passed

C:\fyicenter> java -classpath \
   .;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
   \fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
   \fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
   \fyicenter\selenium\java\libs\okio-1.14.0.jar;\
   \fyicenter\selenium\java\libs\commons-exec-1.3.jar \
   WebDriverLoader Edge
[20:02:53.307] - Listening on http://localhost:48540/
8:02:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[20:02:58.828] - Stopping server.
Test Case - Verify Page Title:
   Actual Value: FYI Center for Software QA Testing
   Expected Value: FYI Center...
   Test Result: Passed

As you can see, the getTitle() method works nicely.

 

Retrieve Web Form Elements with WebDriver in Java

Load WebDriver for Different Browsers in Java

Using Selenium WebDriver Client Java API

⇑⇑ Selenium Tutorials

2020-01-04, 167👍, 0💬