Italian Trulli

How To Verify Page Title in Selenium Using Java

Test Tribe
.

Handling Page Title In Selenium Webdriver 

How to verify page title in Selenium web driver using java

  • How To Get the Page Title in Selenium Webdriver?

driver.getTitle()
By using the getTitle() Method We can get the title of a webpage in Selenium.

Syntax-
The Basic Syntax for getting a title  in selenium is given below

driver.getTitle()


  • Selenium Code To get the page Title Using Java

Selenium Code:

package automationtesting;

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

public class Gettitle {
       public static void main(String[] argsthrows InterruptedException {
              // TODO Auto-generated method stub
             
              System.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");
              WebDriver drivernew ChromeDriver();
              driver.manage().window().maximize();
              driver.get("https://www.facebook.com/");
             
              //Get The Title
              String titl=driver.getTitle();
              
              System.out.println( titl);
              }

                    

}



  • How To Verify Page Title in Selenium Webdriver Using Java
To verify that the actual page title of the webpage is the same as the expected page title just follow the below code.

Selenium Code:

package automationtesting;

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

public class Gettitle {
       public static void main(String[] argsthrows InterruptedException {
              // TODO Auto-generated method stub
             
              System.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");
              WebDriver drivernew ChromeDriver();
              driver.manage().window().maximize();
              driver.get("https://www.facebook.com/");
              System.out.println( driver.getTitle());
              //Get The Title
              String titl=driver.getTitle();
              //Verify The Title
              if (titl.equals("Facebook – log in or sign up"))
              {
                     System.out.println("True");
              }
                     else {
                           System.out.println("False");
                     }
              }

                    

}

Example - Selenium Find page title

WebDriver driver = new ChromeDriver(); 
driver.get("https://www.qaacharya.in"); 
System.out.println(driver.getTitle());

Above Java Selenium code. It opens QA acharya Chrome browser and then prints the page title to the console.


Verify Page title in Selenium Webdriver



Second Way: To Verify the page title in Selenium 

driver.getTitle().contain(“text”); 

Selenium Code 

@Test
public void testTitleReliability() {

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

    boolean title = driver.getTitle().contains("Google");

    if(title)

        System.out.println("I am working correctly");

    else if(!title)

        System.out.println("I am broken!");

Hope!!! The above Tutorial on "How to Get the Page Title in Selenium" is helpful for you...

Team, 
QA acharya

Tags: Page title in selenium, get page title in selenium web driver, Get page title of the web page using selenium web driver. how to verify page title in Selenium web driver


Post a Comment

0 Comments