.
Handling Page Title In Selenium Webdriver
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[] args) throws InterruptedException {// TODO Auto-generated method stubSystem.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");WebDriver driver= new ChromeDriver();driver.manage().window().maximize();driver.get("https://www.facebook.com/");//Get The page TitleString 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[] args) throws InterruptedException {// TODO Auto-generated method stubSystem.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");WebDriver driver= new ChromeDriver();driver.manage().window().maximize();driver.get("https://www.facebook.com/");System.out.println( driver.getTitle());//Get The TitleString titl=driver.getTitle();//Verify The Titleif (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.
Second Way:
driver.getTitle().contain(“text”);
Selenium Code
@Testpublic 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!");
Team,
QA acharya
0 Comments