0
0
Selenium Pythontesting~5 mins

Selenium components (WebDriver, Grid, IDE) in Selenium Python

Choose your learning style9 modes available
Introduction

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 correct
assert 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.