While automating the web application using Selenium, sometimes we need to verify the web page title. In Selenium to verify page titles selenium webdrivers provide a predefined method getTitle().

With the help of the get title method, we can verify the page title in Selenium Webdriver.

 

 

Verify Page Title in Selenium Webdriver
Verify Page Title in Selenium Webdriver

How to Get Page Titles with Selenium WebDriver using Java?

getTilte(): get title method in selenium is a predefined method used to get the webpage’s title.

Syntax: driver.getTitle();

Selenium  Code:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

public class PageTitle{

public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”,“C:\Users\ghs6kor\Desktop\Java\chromedriver.exe”);

WebDriver driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

driver.get(“Enter URL”);

// getTitle() to obtain page title

System.out.println(“Page title is : “ + driver.getTitle());

driver.close();

}

}

The above selenium code will get the page title of the webpage.

How to Verify page title with Selenium WebDriver using Java.

Selenium Code:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Verify_Page_Title {

public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”, “D:\\chromedriver.exe”);

WebDriver driver = new ChromeDriver();

driver.get(“Enter URL”);

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

String Actual = driver.getTitle();

String Expected = “Here enter the page title of webpage”;

if (Actual.equals(Expected))

{

System.out.println(“Test Passed!”);

}

else

{

System.out.println(“Test Failed”);

}

driver.close();

}

}

The above Selenium Code will do the Following process:
  • First, it will Launch the browser
  • Then It will Get the page title
  • Then it will compare the actual page title with the expected page title
  • Then it will close the browser

 

Also Read:

How to verify page title in selenium web driver

Automation QA

Content Team

By Admin

Welcome to Automation QA, your ultimate destination for comprehensive tutorials on automation across various domains. Whether you're a seasoned QA professional looking to enhance your skills or a newcomer eager to delve into the world of automation testing, our platform has something for everyone.

Leave a Reply

Your email address will not be published. Required fields are marked *