Selenium getTitle() method to get title of page

Selenium get title using getTitle() method

If you view source of page, You will find title of the page inside <title> and </title> tags. Selenium getTitle() method is useful to get the title of the page. You can verify or assert that title in selenium test case to check if targeted page is open or not.
  1. getTitle() method is part of WebDriver interface which extends SearchContext interface.
  2. It will return title of the web page if title is available for page.
  3. It will return null string if title of the page is missing.
  4. Syntax to get title : driver.getTitle();
Selenium getTitle() Method

Example of selenium getTitle() method

In below given example, We will open test site page and get the title. We will print that title in console.

package testPackage;

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

public class getTitleExample {

	public static void main(String[] args) {
				
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		
		driver.navigate().to("http://www.only-testing-blog.blogspot.com");
		
		//Get the title of page and print it in console.
		String pageTitle = driver.getTitle();
		System.out.println("Page title is : "+pageTitle);	
	}
}

In console, It will print the title of the web page.

No comments:

Post a Comment