B. findElement should use By.name instead of By.id
C. driver.close() should be driver.quit() to close browser fully
D. button.click() is not valid for WebElement
Solution
Step 1: Understand driver.close() vs driver.quit()
driver.close() closes current tab, driver.quit() closes entire browser session.
Step 2: Check best practice for test cleanup
To fully close browser and end session, driver.quit() is preferred after test.
Final Answer:
driver.close() should be driver.quit() to close browser fully -> Option C
Quick Check:
Use quit() to close browser fully [OK]
Hint: Use driver.quit() to close browser fully after test [OK]
Common Mistakes:
Using driver.close() only closes one tab
Confusing By.id and By.name without context
Assuming button.click() is invalid
5. You want to write a Selenium Java test that opens a browser, navigates to "https://example.com", verifies the page title is "Example Domain", and then closes the browser. Which code snippet correctly performs this test with assertion?
hard
A. WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
assert driver.getTitle().equals("Example Domain");
driver.quit();
B. WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
if(driver.getTitle() == "Example Domain") {
System.out.println("Title matches");
}
driver.close();
C. WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
assert driver.getTitle() == "Example Domain";
driver.quit();
D. WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
assertEquals("Example Domain", driver.getTitle());
driver.quit();
Solution
Step 1: Check assertion method correctness
assertEquals(expected, actual) is the correct JUnit assertion method to compare strings.
Step 2: Verify browser close method
driver.quit() properly closes the browser after test.
Step 3: Identify errors in other options
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
assert driver.getTitle().equals("Example Domain");
driver.quit(); uses plain assert with equals() but is less clear; WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
if(driver.getTitle() == "Example Domain") {
System.out.println("Title matches");
}
driver.close(); uses == for string comparison (wrong); WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
assert driver.getTitle() == "Example Domain";
driver.quit(); uses == in assert (wrong).
Final Answer:
assertEquals("Example Domain", driver.getTitle()); with driver.quit() -> Option D
Quick Check:
Use assertEquals for string check, quit() to close browser [OK]
Hint: Use assertEquals(expected, actual) and driver.quit() [OK]
Common Mistakes:
Using == to compare strings
Using driver.close() instead of quit()
Missing proper assertion method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.