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.
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();
}
}
- 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