Selenium WebDriver is used to automate web browsers. It helps testers simulate user actions like clicking buttons, entering text, and navigating pages to check if a website works correctly.
Click to reveal answer
beginner
In a Selenium Java test, what does the method driver.get("https://example.com") do?
It opens the web page at the URL "https://example.com" in the browser controlled by Selenium WebDriver.
Click to reveal answer
beginner
Why do we use driver.quit() at the end of a Selenium test?
To close the browser and end the WebDriver session. This frees up system resources and ensures the test ends cleanly.
Click to reveal answer
beginner
What is the purpose of assertions in a Selenium test?
Assertions check if the actual result matches the expected result. They help confirm that the web page behaves as intended during the test.
Click to reveal answer
beginner
Which locator strategy is recommended for finding elements in Selenium tests?
Using unique and stable locators like id or name attributes is recommended. They make tests more reliable and easier to maintain.
Click to reveal answer
What does the following Selenium Java code do?
driver.findElement(By.id("submitBtn")).click();
ACloses the browser
BFinds a button with id 'submitBtn' and clicks it
COpens a new browser window
DTypes text 'submitBtn' into a field
✗ Incorrect
The code finds the element with id 'submitBtn' and performs a click action on it.
Which method opens a web page in Selenium WebDriver?
Adriver.get()
Bdriver.open()
Cdriver.navigate()
Ddriver.load()
✗ Incorrect
The method driver.get() loads a web page by URL in the browser.
Why is it important to use driver.quit() after a test?
ATo refresh the page
BTo start a new test
CTo close the browser and end the session
DTo maximize the browser window
✗ Incorrect
driver.quit() closes the browser and ends the WebDriver session, freeing resources.
Which locator is usually the most reliable in Selenium tests?
AUnique id attribute
BXPath with long paths
CCSS selectors with many classes
DText content of elements
✗ Incorrect
Unique id attributes are stable and fast for locating elements.
What is the main purpose of assertions in Selenium tests?
ATo close the browser
BTo open the browser
CTo find elements on the page
DTo check if the test passed or failed
✗ Incorrect
Assertions verify if the actual result matches the expected result, determining test success.
Describe the basic steps to write and run your first Selenium Java test.
Think about what you do when you test a website manually and how Selenium automates those steps.
You got /7 concepts.
Explain why choosing good locators is important in Selenium tests.
Imagine trying to find a friend in a crowd using a clear description versus a vague one.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Selenium Java test?
easy
A. To write Java programs unrelated to browsers
B. To automate browser actions like opening a page and checking content
C. To manually test software without code
D. To design user interfaces
Solution
Step 1: Understand Selenium's role
Selenium is used to automate browser actions such as opening pages and clicking buttons.
Step 2: Identify the test goal
The main goal of a Selenium Java test is to automate browser tasks and verify web page behavior.
Final Answer:
To automate browser actions like opening a page and checking content -> Option B
Quick Check:
Selenium test = automate browser [OK]
Hint: Selenium automates browsers, not manual or UI design tasks [OK]
Common Mistakes:
Confusing Selenium with manual testing
Thinking Selenium is for UI design
Assuming Selenium runs non-browser Java code
2. Which of the following is the correct way to start a Chrome browser in Selenium Java?
easy
A. WebDriver driver = new ChromeDriver();
B. ChromeDriver driver = WebDriver();
C. driver = new WebDriver.Chrome();
D. WebDriver driver = ChromeDriver();
Solution
Step 1: Recall Selenium WebDriver syntax
The correct way is to declare WebDriver interface and instantiate ChromeDriver class.
Step 2: Check each option's syntax
WebDriver driver = new ChromeDriver(); uses correct Java syntax: WebDriver driver = new ChromeDriver(); others have syntax errors or wrong order.
Final Answer:
WebDriver driver = new ChromeDriver(); -> Option A
Quick Check:
Use interface = new implementation() [OK]
Hint: Use WebDriver driver = new ChromeDriver(); to start Chrome [OK]
Common Mistakes:
Missing 'new' keyword
Wrong constructor call syntax
Assigning without type declaration
3. What will be the output of this Selenium Java code snippet?
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String title = driver.getTitle();
System.out.println(title);
driver.quit();
Assuming the page title is "Example Domain".
medium
A. Example Domain
B. https://example.com
C. null
D. No output, code throws error
Solution
Step 1: Understand driver.getTitle()
driver.getTitle() returns the title of the current page, which is "Example Domain" here.
Step 2: Check the print statement
System.out.println(title) prints the page title to the console.
Final Answer:
Example Domain -> Option A
Quick Check:
getTitle() prints page title [OK]
Hint: getTitle() returns page title string [OK]
Common Mistakes:
Confusing URL with title
Expecting no output
Assuming getTitle() returns null
4. Identify the error in this Selenium Java code snippet:
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.