Selenium helps us test websites automatically. It has parts that do different jobs to make testing easier and faster.
You want to check if a website works well on different browsers.
You need to run many tests at the same time on different computers.
You want to record your actions on a website to create a test without coding.
You want to control a browser using code to test website features.
You want to share tests with your team easily.
Syntax
Selenium Python
WebDriver: Controls browsers using code.
Grid: Runs tests on many machines and browsers at once.
IDE: Records and plays back tests without coding.
WebDriver is the main tool to write automated tests in code.
Grid helps run tests faster by using many computers.
IDE is good for beginners to create tests by clicking.
Examples
This example shows how WebDriver opens a Chrome browser and visits a website.
Selenium Python
from selenium import webdriver
# Open Chrome browser
driver = webdriver.Chrome()
driver.get('https://example.com')
driver.quit()
This example shows how to run a test on Selenium Grid by connecting to a remote server.
Selenium Python
# Selenium Grid setup is done outside code, but you connect like this:from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Connect to remote Grid server
driver = webdriver.Remote(
command_executor='http://grid-server:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
driver.get('https://example.com')
driver.quit()
Selenium IDE lets you record tests by clicking in the browser. You can export tests to code later.
Selenium Python
# Selenium IDE is a browser extension, no code needed.# You record actions and export tests if needed.
Sample Program
This test opens a browser, goes to a website, checks the main heading text, and closes the browser. It uses WebDriver to control the browser.
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Open Chrome browser
driver = webdriver.Chrome()
# Go to example website
driver.get('https://example.com')
# Find the heading element
heading = driver.find_element(By.TAG_NAME, 'h1')
# Check if heading text is correctassert heading.text == 'Example Domain', 'Heading text does not match'print('Test passed: Heading text is correct')
# Close browser
driver.quit()
OutputSuccess
Important Notes
Always close the browser with driver.quit() to free resources.
Selenium Grid requires setup of a hub and nodes before use.
Selenium IDE is great for beginners but has limits for complex tests.
Summary
Selenium has three main parts: WebDriver (code control), Grid (parallel testing), and IDE (record tests).
Use WebDriver to write tests in code for flexibility.
Use Grid to run many tests faster on different machines.
Practice
(1/5)
1. Which Selenium component allows you to write test scripts in programming languages like Python?
easy
A. WebDriver
B. Grid
C. IDE
D. Selenium Server
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 A
Quick Check:
Write tests in code = WebDriver [OK]
Hint: Code tests? Think WebDriver [OK]
Common Mistakes:
Confusing Grid with writing code
Thinking IDE is for coding tests
Selecting Selenium Server which is not a main component
2. Which of the following is the correct way to start a Chrome WebDriver session in Python?
easy
A. driver = webdriver.start('chrome')
B. driver = webdriver.Chrome()
C. driver = ChromeDriver()
D. driver = webdriver.open('chrome')
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 B
Quick Check:
Correct constructor = webdriver.Chrome() [OK]
Hint: Use webdriver.Chrome() to start Chrome [OK]
Common Mistakes:
Using undefined methods like start() or open()
Trying to instantiate ChromeDriver directly
Missing webdriver prefix
3. What will be the output of the following Python Selenium code snippet?
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)
medium
A. NoSuchElementException
B. submit
C. id
D. button
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 A
Quick Check:
No element on page = NoSuchElementException [OK]
Hint: Verify element exists on the actual page [OK]
Common Mistakes:
Assuming example.com has id='submit'
Confusing element ID with tag name
Expecting print to run despite missing element
4. You want to run tests on multiple machines at the same time using Selenium. You wrote this code but tests run only on one machine. What is missing?