We get the page title and URL to check if we are on the right webpage during testing.
Page title and URL retrieval in Selenium Java
WebDriver driver = new ChromeDriver();
// Open a webpage
driver.get("https://example.com");
// Get page title
String pageTitle = driver.getTitle();
// Get current URL
String currentUrl = driver.getCurrentUrl();Use getTitle() to get the page title as a String.
Use getCurrentUrl() to get the current page URL as a String.
String title = driver.getTitle();
System.out.println("Title: " + title);String url = driver.getCurrentUrl();
System.out.println("URL: " + url);driver.get("https://www.google.com"); String title = driver.getTitle(); String url = driver.getCurrentUrl(); System.out.println("Title: " + title); System.out.println("URL: " + url);
This program opens the browser, navigates to example.com, prints the page title and URL, then closes the browser.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class PageInfoTest { public static void main(String[] args) { // Set path to chromedriver executable if needed // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // Open example.com driver.get("https://example.com"); // Get and print page title String pageTitle = driver.getTitle(); System.out.println("Page Title: " + pageTitle); // Get and print current URL String currentUrl = driver.getCurrentUrl(); System.out.println("Current URL: " + currentUrl); // Close the browser driver.quit(); } }
Getting the title and URL is fast and uses little memory (O(1) time and space).
Common mistake: calling these methods before the page fully loads can give wrong results.
Use these methods to verify navigation success instead of relying only on page content.
Use getTitle() to get the page title as a string.
Use getCurrentUrl() to get the current page URL as a string.
Check these values to confirm you are on the expected webpage during tests.
Practice
Solution
Step 1: Understand the purpose of
This method returns the title of the current webpage as a string, which helps verify the page loaded.getTitle()Step 2: Compare with other methods
getCurrentUrl()returns the URL, not the title. The other options are not valid Selenium methods.Final Answer:
getTitle() -> Option AQuick Check:
Page title retrieval = getTitle() [OK]
- Confusing getTitle() with getCurrentUrl()
- Using non-existent methods like getPageTitle()
- Trying to get title from URL method
driver?Solution
Step 1: Identify the correct method name
The correct Selenium method to get the current URL isgetCurrentUrl().Step 2: Check Java syntax correctness
Assigning the result to a String variable withString url = driver.getCurrentUrl();is correct syntax.Final Answer:
String url = driver.getCurrentUrl(); -> Option DQuick Check:
URL retrieval syntax = getCurrentUrl() [OK]
- Using getUrl() instead of getCurrentUrl()
- Missing String type declaration
- Calling methods without parentheses
String title = driver.getTitle(); String url = driver.getCurrentUrl(); System.out.println(title); System.out.println(url);
Solution
Step 1: Understand method outputs
getTitle()returns the page title "Welcome Page" andgetCurrentUrl()returns the URL "https://example.com/home".Step 2: Check print order
The code prints title first, then URL, so output matches Welcome Page https://example.com/home exactly.Final Answer:
Welcome Page https://example.com/home -> Option CQuick Check:
Title then URL printed = Welcome Page https://example.com/home [OK]
- Mixing up title and URL order in output
- Assuming methods return null without page loaded
- Confusing getTitle() with getCurrentUrl() output
System.out.println(driver.gettitle()); System.out.println(driver.getCurrentUrl);
Solution
Step 1: Check method name case sensitivity
Java is case-sensitive;gettitle()should begetTitle().Step 2: Verify method call syntax
getCurrentUrlis missing parentheses(), so it is not a valid method call.Final Answer:
Method names are case-sensitive and parentheses are missing -> Option BQuick Check:
Correct method names and parentheses needed [OK]
- Ignoring case sensitivity in method names
- Forgetting parentheses on method calls
- Assuming methods return void incorrectly
Solution
Step 1: Verify title assertion
assertEquals(driver.getTitle(), "Dashboard")correctly checks exact title match.Step 2: Verify URL assertion
assertTrue(driver.getCurrentUrl().contains("dashboard"))correctly checks URL contains substring "dashboard".Final Answer:
assertEquals(driver.getTitle(), "Dashboard"); assertTrue(driver.getCurrentUrl().contains("dashboard")); -> Option AQuick Check:
Exact title and URL substring check = assertEquals(driver.getTitle(), "Dashboard"); assertTrue(driver.getCurrentUrl().contains("dashboard")); [OK]
- Using equals for URL when substring check needed
- Mixing case sensitivity in title and URL checks
- Using contains for title when exact match required
Ready to prove it?
Pass the test and this pattern moves unknown
