Selenium components (WebDriver, Grid, IDE) in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def test_google_search(): # Initialize Chrome WebDriver driver = webdriver.Chrome() try: # Open Google homepage driver.get('https://www.google.com') # Wait until the search box is present wait = WebDriverWait(driver, 10) search_box = wait.until(EC.presence_of_element_located((By.NAME, 'q'))) # Enter search term and submit search_term = 'Selenium WebDriver' search_box.send_keys(search_term + Keys.RETURN) # Wait until title contains search term wait.until(EC.title_contains(search_term)) # Assert title contains search term assert search_term in driver.title, f"Title does not contain '{search_term}'" finally: # Close the browser driver.quit() if __name__ == '__main__': test_google_search()
This script uses Selenium WebDriver with Python to automate the manual test case.
First, it opens the Chrome browser and navigates to Google.
It waits explicitly for the search input field to appear using WebDriverWait and expected_conditions.
Then it types the search term and submits the form by sending the RETURN key.
Next, it waits until the page title contains the search term to ensure the results page loaded.
Finally, it asserts the title contains the search term and closes the browser in a finally block to ensure cleanup.
This approach follows best practices: using explicit waits, proper locators, and clean resource management.
Now add data-driven testing with 3 different search terms: 'Selenium WebDriver', 'Python testing', 'Automation QA'
Practice
Solution
Step 1: Understand Selenium components
WebDriver is the part of Selenium that lets you write code to control browsers.Step 2: Match component to functionality
Grid is for running tests in parallel, IDE is for recording tests, so only WebDriver fits writing code tests.Final Answer:
WebDriver -> Option AQuick Check:
Write tests in code = WebDriver [OK]
- Confusing Grid with writing code
- Thinking IDE is for coding tests
- Selecting Selenium Server which is not a main component
Solution
Step 1: Recall Selenium Python syntax
The standard way to start Chrome WebDriver is using webdriver.Chrome() constructor.Step 2: Check other options for syntax errors
Options A, C, and D use incorrect method names or classes not defined in Selenium Python.Final Answer:
driver = webdriver.Chrome() -> Option BQuick Check:
Correct constructor = webdriver.Chrome() [OK]
- Using undefined methods like start() or open()
- Trying to instantiate ChromeDriver directly
- Missing webdriver prefix
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.ID, 'submit')
print(element.tag_name)Solution
Step 1: Understand the code behavior
The code opens example.com and tries to find an element with ID 'submit'.Step 2: Check the target page
The example.com page does not contain an element with id='submit', so driver.find_element raises NoSuchElementException before the print statement executes.Final Answer:
NoSuchElementException -> Option AQuick Check:
No element on page = NoSuchElementException [OK]
- Assuming example.com has id='submit'
- Confusing element ID with tag name
- Expecting print to run despite missing element
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
driver.get('https://example.com')Solution
Step 1: Identify the problem context
The code runs tests on one machine only, but the goal is parallel testing on multiple machines.Step 2: Understand Selenium Grid role
Selenium Grid allows running tests on many machines simultaneously. The code must connect to the Grid hub to distribute tests.Final Answer:
You need to configure Selenium Grid and connect to it -> Option CQuick Check:
Parallel tests require Selenium Grid setup [OK]
- Assuming importing a module enables Grid
- Switching browsers does not enable parallelism
- Using IDE instead of Grid for parallel runs
Solution
Step 1: Identify the requirement
The user wants to record tests without writing code and export to Python WebDriver code.Step 2: Match Selenium component to feature
Selenium IDE is a browser plugin that records user actions and can export recorded tests to code in various languages including Python WebDriver.Final Answer:
Selenium IDE -> Option DQuick Check:
Record tests and export code = Selenium IDE [OK]
- Confusing Grid with recording tests
- Thinking WebDriver records tests
- Assuming Selenium Server handles recording
Ready to prove it?
Pass the test and this pattern moves unknown
