
.
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 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[] 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 TitleString 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[] 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: 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!");
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
0 Comments