Bird
Raised Fist0
Selenium Javatesting~20 mins

Page title and URL retrieval in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Page Title & URL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium WebDriver code that opens a webpage and retrieves its title and URL. What will be printed to the console?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String title = driver.getTitle();
String url = driver.getCurrentUrl();
System.out.println(title + " - " + url);
driver.quit();
AExample Domain - https://example.com
Bhttps://example.com - Example Domain
Cnull - null
DThrows NoSuchElementException
Attempts:
2 left
💡 Hint
Remember that getTitle() returns the page title and getCurrentUrl() returns the current URL loaded in the browser.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the page title in Selenium Java?
You want to verify that the page title is exactly "Welcome Page" after loading the page. Which assertion is correct?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://mysite.com/welcome");
String actualTitle = driver.getTitle();
AassertTrue(actualTitle.contains("Welcome Page"));
BassertFalse(actualTitle.isEmpty());
CassertNotNull(actualTitle);
DassertEquals("Welcome Page", actualTitle);
Attempts:
2 left
💡 Hint
You want to check exact equality, not just presence or non-null.
❓ locator
advanced
1:30remaining
Identify the best locator to verify the current URL contains a specific path
You want to write a test that verifies the current URL contains the path "/dashboard" after login. Which Selenium Java code snippet correctly retrieves the URL for this check?
AString url = driver.findElement(By.id("url")).getText();
BString url = driver.getCurrentUrl();
CString url = driver.getTitle();
DString url = driver.getPageSource();
Attempts:
2 left
💡 Hint
The URL is not an element on the page but a property of the browser.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Java test fail to get the page title?
Examine the code below. The test throws a NullPointerException when trying to get the title. What is the most likely cause?
Selenium Java
WebDriver driver;
driver.get("https://example.com");
String title = driver.getTitle();
System.out.println(title);
Adriver was never initialized with a browser instance
BgetTitle() method is deprecated and throws exception
CThe URL https://example.com is unreachable
DMissing driver.quit() causes NullPointerException
Attempts:
2 left
💡 Hint
Check if the WebDriver object is assigned before use.
❓ framework
expert
2:30remaining
In a Selenium Java test framework, which method is best to retrieve and log page title and URL after each test?
You want to automatically log the page title and URL after every test method runs in your Selenium Java test suite using TestNG. Which approach is best?
AUse @BeforeClass annotated method to log title and URL before tests run
BCall driver.getTitle() and driver.getCurrentUrl() inside every @Test method manually
CUse @AfterMethod annotated method to call driver.getTitle() and driver.getCurrentUrl() and log them
DUse a static block to log title and URL once when class loads
Attempts:
2 left
💡 Hint
Think about running code after each test method automatically.

Practice

(1/5)
1. Which Selenium WebDriver method is used to retrieve the title of the current webpage as a string?
easy
A. getTitle()
B. getPageTitle()
C. getCurrentUrl()
D. getUrl()

Solution

  1. Step 1: Understand the purpose of getTitle()

    This method returns the title of the current webpage as a string, which helps verify the page loaded.
  2. Step 2: Compare with other methods

    getCurrentUrl() returns the URL, not the title. The other options are not valid Selenium methods.
  3. Final Answer:

    getTitle() -> Option A
  4. Quick Check:

    Page title retrieval = getTitle() [OK]
Hint: Remember: Title uses getTitle(), URL uses getCurrentUrl() [OK]
Common Mistakes:
  • Confusing getTitle() with getCurrentUrl()
  • Using non-existent methods like getPageTitle()
  • Trying to get title from URL method
2. Which of the following is the correct Java syntax to get the current page URL using Selenium WebDriver stored in variable driver?
easy
A. String url = driver.getUrl();
B. String url = driver.currentUrl();
C. String url = driver.getPageUrl();
D. String url = driver.getCurrentUrl();

Solution

  1. Step 1: Identify the correct method name

    The correct Selenium method to get the current URL is getCurrentUrl().
  2. Step 2: Check Java syntax correctness

    Assigning the result to a String variable with String url = driver.getCurrentUrl(); is correct syntax.
  3. Final Answer:

    String url = driver.getCurrentUrl(); -> Option D
  4. Quick Check:

    URL retrieval syntax = getCurrentUrl() [OK]
Hint: Use getCurrentUrl() exactly as shown for URL retrieval [OK]
Common Mistakes:
  • Using getUrl() instead of getCurrentUrl()
  • Missing String type declaration
  • Calling methods without parentheses
3. What will be the output of the following code snippet if the current page title is "Welcome Page" and URL is "https://example.com/home"?
String title = driver.getTitle();
String url = driver.getCurrentUrl();
System.out.println(title);
System.out.println(url);
medium
A. null null
B. https://example.com/home Welcome Page
C. Welcome Page https://example.com/home
D. Welcome Page null

Solution

  1. Step 1: Understand method outputs

    getTitle() returns the page title "Welcome Page" and getCurrentUrl() returns the URL "https://example.com/home".
  2. Step 2: Check print order

    The code prints title first, then URL, so output matches Welcome Page https://example.com/home exactly.
  3. Final Answer:

    Welcome Page https://example.com/home -> Option C
  4. Quick Check:

    Title then URL printed = Welcome Page https://example.com/home [OK]
Hint: Print order matches method call order: title then URL [OK]
Common Mistakes:
  • Mixing up title and URL order in output
  • Assuming methods return null without page loaded
  • Confusing getTitle() with getCurrentUrl() output
4. Identify the error in the following Selenium Java code snippet that tries to print the page title and URL:
System.out.println(driver.gettitle());
System.out.println(driver.getCurrentUrl);
medium
A. getCurrentUrl() returns void, cannot print
B. Method names are case-sensitive and parentheses are missing
C. driver object is not declared
D. No error, code will run fine

Solution

  1. Step 1: Check method name case sensitivity

    Java is case-sensitive; gettitle() should be getTitle().
  2. Step 2: Verify method call syntax

    getCurrentUrl is missing parentheses (), so it is not a valid method call.
  3. Final Answer:

    Method names are case-sensitive and parentheses are missing -> Option B
  4. Quick Check:

    Correct method names and parentheses needed [OK]
Hint: Check method names exactly and include () for calls [OK]
Common Mistakes:
  • Ignoring case sensitivity in method names
  • Forgetting parentheses on method calls
  • Assuming methods return void incorrectly
5. You want to verify that your Selenium test is on the correct page by checking both the page title and URL. Which of the following code snippets correctly asserts that the title is "Dashboard" and the URL contains "dashboard"?
hard
A. assertEquals(driver.getTitle(), "Dashboard"); assertTrue(driver.getCurrentUrl().contains("dashboard"));
B. assertTrue(driver.getTitle().equals("Dashboard")); assertEquals(driver.getCurrentUrl(), "dashboard");
C. assertEquals(driver.getTitle(), "dashboard"); assertTrue(driver.getCurrentUrl().contains("Dashboard"));
D. assertTrue(driver.getTitle().contains("Dashboard")); assertTrue(driver.getCurrentUrl().equals("dashboard"));

Solution

  1. Step 1: Verify title assertion

    assertEquals(driver.getTitle(), "Dashboard") correctly checks exact title match.
  2. Step 2: Verify URL assertion

    assertTrue(driver.getCurrentUrl().contains("dashboard")) correctly checks URL contains substring "dashboard".
  3. Final Answer:

    assertEquals(driver.getTitle(), "Dashboard"); assertTrue(driver.getCurrentUrl().contains("dashboard")); -> Option A
  4. Quick Check:

    Exact title and URL substring check = assertEquals(driver.getTitle(), "Dashboard"); assertTrue(driver.getCurrentUrl().contains("dashboard")); [OK]
Hint: Use assertEquals for exact title, assertTrue with contains for URL [OK]
Common Mistakes:
  • Using equals for URL when substring check needed
  • Mixing case sensitivity in title and URL checks
  • Using contains for title when exact match required
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard