Table of content

getTitle

We can get the title of a page using getTitle(), it returns title as string

page title : chercher.tech


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class GetTitle 
						{
							public static void main(String[] args) throws Exception 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
								System.out.println("Page title is : "+driver.getTitle());							
							}
						}

					

Output: Page title is CherCher Tech | Learning is fun

Open Google.com and verify whether the title is Google or not ?

						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class Refresh 
						{
							public static void main(String[] args) throws Exception 
							{
								System.setProperty("webdriver.firefox.driver", "D:\\PATH\\geckodriver.exe");
								WebDriver driver=new FirefoxDriver();
								driver.get("https://google.com");
								if (driver.getTitle().equals("Google")) 
								{
									System.out.println("Title is Google ");
								}
								else 
								{
									System.out.println("Title is not Google");
								}							
							}
						}
					

Likewise, you can also compare whether title has Google as a part of the title or not

Take Element Screenshot

Get URL

We can get the URL of the page the using getCurrentUrl(), it returns URL as a String

page url


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class GetUrl 
						{
							public static void main(String[] args) throws Exception 
							{
								System.setProperty("webdriver.firefox.driver", "D:\\PATH\\geckodriver.exe");
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
								// below line gets the url of a website
								driver.getCurrentUrl();									
							}
						}
					

More than Two Browser Windows

Get Page Source

We can get the page source of a page using getPageSource(), it returns page source of the as a String.


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class PageSource 
						{
							public static void main(String[] args) throws Exception 
							{
								System.setProperty("webdriver.firefox.driver", "D:\\PATH\\geckodriver.exe");
								WebDriver driver=new FirefoxDriver();	
								driver.get("https://chercher.tech");			
								driver.getPageSource();							
							}
						}

					

Handle dropdowns in selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions