First Selenium script in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException # Setup ChromeDriver service service = Service() # Create a new Chrome browser instance with webdriver.Chrome(service=service) as driver: try: # Navigate to Google homepage driver.get('https://www.google.com') # Wait up to 10 seconds for the title to be 'Google' WebDriverWait(driver, 10).until(EC.title_is('Google')) # Assert the page title assert driver.title == 'Google', f"Expected title 'Google' but got '{driver.title}'" print('Test Passed: Page title is Google') except TimeoutException: print('Test Failed: Page title did not become Google in time') except AssertionError as e: print(f'Test Failed: {e}')
This script uses Selenium WebDriver with Python to open the Chrome browser and navigate to the Google homepage.
We use WebDriverWait with expected_conditions.title_is to wait explicitly until the page title becomes exactly 'Google'. This avoids timing issues where the page might not be fully loaded yet.
The assertion checks that the title is exactly 'Google'. If it is, the test prints a success message; otherwise, it prints a failure message.
The with statement ensures the browser closes automatically after the test finishes, even if an error occurs.
This approach follows best practices by using explicit waits, proper assertions, and clean browser management.
Now add data-driven testing to open Google and verify the title for three different URLs: 'https://www.google.com', 'https://www.google.co.uk', and 'https://www.google.ca'.
Practice
Solution
Step 1: Understand Selenium's role
Selenium is a tool used to control browsers automatically for testing purposes.Step 2: Match purpose with options
Only To automate browser actions for testing websites describes automating browser actions to test websites, which is Selenium's main use.Final Answer:
To automate browser actions for testing websites -> Option AQuick Check:
Selenium automates browsers = B [OK]
- Confusing Selenium with manual testing
- Thinking Selenium designs websites
- Mixing Selenium with database creation
Solution
Step 1: Recall Selenium syntax for Chrome
In Selenium Python, opening Chrome uses webdriver.Chrome() constructor.Step 2: Check options for correct syntax
Only driver = webdriver.Chrome() uses the correct constructor. Others are either for different browsers or invalid methods.Final Answer:
driver = webdriver.Chrome() -> Option CQuick Check:
Open Chrome with webdriver.Chrome() = C [OK]
- Using webdriver.Firefox() for Chrome
- Calling non-existent methods like open() or start()
- Missing parentheses after Chrome
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.TAG_NAME, 'h1')
print(element.text)
driver.quit()Solution
Step 1: Understand code actions
The code opens Chrome, navigates to example.com, finds the first <h1> element, prints its text, then closes the browser.Step 2: Analyze expected output
example.com has an <h1> with text 'Example Domain', so element.text prints that text.Final Answer:
Prints the text inside the first <h1> tag on example.com -> Option DQuick Check:
find_element(By.TAG_NAME, 'h1') gets <h1> text = D [OK]
- Thinking By.TAG_NAME is invalid
- Expecting URL printed instead of element text
- Assuming element.text is empty without checking page
from selenium import webdriver
driver = webdriver.Chrome
driver.get('https://example.com')
driver.quit()Solution
Step 1: Check webdriver.Chrome usage
webdriver.Chrome is a class; to create a driver object, parentheses are needed: webdriver.Chrome()Step 2: Review other lines for errors
URL format is correct, driver.quit() is after get().Step 3: Determine main error
Missing parentheses causes driver to be a class, not an instance, so driver.get() will fail.Final Answer:
Missing parentheses after webdriver.Chrome to create driver instance -> Option AQuick Check:
webdriver.Chrome() needs parentheses = A [OK]
- Forgetting parentheses after webdriver.Chrome
- Thinking URL format is wrong
- Confusing the order of driver.get() and driver.quit()
Solution
Step 1: Check correct assertion syntax
In Python, assert uses '==' to compare values, so 'assert driver.title == "Example Domain"' is correct.Step 2: Verify method and property names
driver.title is the correct property for page title; driver.getTitle() is incorrect in Python Selenium.Step 3: Confirm logic correctness
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') assert driver.title == 'Example Domain' driver.quit() asserts title equals 'Example Domain', which matches the requirement.Final Answer:
Correct assertion with driver.title == 'Example Domain' -> Option BQuick Check:
Use assert driver.title == 'Example Domain' = A [OK]
- Using single '=' instead of '==' in assert
- Using driver.getTitle() instead of driver.title
- Asserting title not equal instead of equal
Ready to prove it?
Pass the test and this pattern moves unknown
