We can get the title of a page using getTitle(), it returns title as string
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:PATHgeckodriver.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
We can get the URL of the page the using getCurrentUrl(), it returns URL as a String
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:PATHgeckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://chercher.tech");
// below line gets the url of a website
driver.getCurrentUrl();
}
}
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:PATHgeckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://chercher.tech");
driver.getPageSource();
}
}
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.