Italian Trulli

How To Get and Verify Page Title in Selenium Webdriver

Test Tribe
.

Handling Page Title In Selenium Webdriver 

How to verify page title in Selenium webdriver 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 get 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 page Title
              String titl=driver.getTitle();
              
              System.out.println( titl);
              }

                    

}



How To Verify Page Title in Selenium Webdriver Using Java?


There are two ways to verify page title in Selenium WebDriver.

To verify that the actual page title of the webpage is the same as the expected page title just follow the below code.

First Way:

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:

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 and Verify Page Title in Selenium Webdriver" is helpful for you...

Team, 
QA acharya

Post a Comment

0 Comments