0
0
Selenium Pythontesting~15 mins

First Selenium script in Selenium Python - Deep Dive

Choose your learning style9 modes available
Overview - First Selenium script
What is it?
A Selenium script is a small program that controls a web browser automatically. It can open websites, click buttons, fill forms, and check if things work as expected. This helps testers check websites without doing everything by hand. Selenium scripts use code to tell the browser what to do step-by-step.
Why it matters
Without Selenium scripts, testers would have to check websites manually, which is slow and prone to mistakes. Automating tests saves time, finds bugs faster, and makes sure websites work well for users. This is important because websites change often and need quick, reliable checks.
Where it fits
Before learning Selenium scripts, you should know basic programming in Python and understand what web browsers and websites are. After this, you can learn how to write more complex tests, use test frameworks, and run tests automatically in teams.
Mental Model
Core Idea
A Selenium script is like a remote control that tells a web browser exactly what to do to test a website automatically.
Think of it like...
Imagine you have a robot helper who follows your exact instructions to open a door, press buttons, and check if the lights turn on. The Selenium script is the instruction list, and the browser is the robot doing the tasks.
┌─────────────────────┐
│ Selenium Script Code │
└──────────┬──────────┘
           │ sends commands
           ▼
┌─────────────────────┐
│   Web Browser (e.g., │
│    Chrome, Firefox)  │
└──────────┬──────────┘
           │ performs actions
           ▼
┌─────────────────────┐
│  Website Under Test  │
└─────────────────────┘
Build-Up - 7 Steps
1
FoundationSetting up Selenium and WebDriver
🤔
Concept: Learn how to install Selenium and prepare the browser driver to control the browser.
First, install Selenium using pip: pip install selenium. Then, download the correct WebDriver for your browser (like ChromeDriver for Chrome). Place the driver in a known folder or add it to your system path. This setup lets your Python code talk to the browser.
Result
You have Selenium installed and the browser driver ready to launch and control the browser.
Knowing how to set up Selenium and the driver is essential because without this, your script cannot communicate with the browser.
2
FoundationWriting a simple script to open a webpage
🤔
Concept: Create a basic script that opens a browser and loads a website.
Import Selenium's webdriver module. Create a browser instance (e.g., Chrome). Use the get() method to open a URL. Finally, close the browser with quit(). Example: from selenium import webdriver browser = webdriver.Chrome() browser.get('https://example.com') browser.quit()
Result
The browser opens, loads the example.com page, then closes.
This step shows how Selenium controls the browser to perform simple tasks, forming the base for all tests.
3
IntermediateFinding elements on the page
🤔Before reading on: do you think Selenium can find page elements only by their visible text or also by other attributes? Commit to your answer.
Concept: Learn how to locate elements on a webpage using different methods like ID, name, or CSS selectors.
Selenium can find elements by many ways: by ID, name, class name, tag name, CSS selector, or XPath. For example, to find a button with ID 'submit': button = browser.find_element('id', 'submit') This lets you interact with specific parts of the page.
Result
You can select any element on the page to click, type into, or check.
Understanding element locators is key because tests depend on interacting with the right parts of the page.
4
IntermediatePerforming actions on elements
🤔Before reading on: do you think Selenium can only click buttons, or can it also type text and read values? Commit to your answer.
Concept: Use Selenium to click buttons, type text into fields, and read text from the page.
Once you find an element, you can: - Click it: element.click() - Type text: element.send_keys('hello') - Read text: text = element.text Example: input_box = browser.find_element('name', 'q') input_box.send_keys('Selenium') input_box.submit()
Result
The script types 'Selenium' into a search box and submits the form.
Knowing how to perform actions lets your tests simulate real user behavior.
5
IntermediateAdding assertions to verify results
🤔Before reading on: do you think Selenium automatically knows if a test passes, or do you need to check conditions yourself? Commit to your answer.
Concept: Add checks in your script to confirm the website behaves as expected using assertions.
After performing actions, check if the page shows the right content. Use Python's assert statement: assert 'Selenium' in browser.title This means the test will fail if 'Selenium' is not in the page title, signaling a problem.
Result
The script confirms the page title contains 'Selenium', passing the test if true.
Assertions turn scripts into tests by verifying expected outcomes, making failures visible.
6
AdvancedHandling waits for dynamic content
🤔Before reading on: do you think Selenium waits automatically for page elements to load, or do you need to tell it to wait? Commit to your answer.
Concept: Learn how to wait for elements to appear before interacting, handling pages that load content slowly.
Websites often load parts after the page appears. Use Selenium's WebDriverWait to pause until elements are ready: from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(browser, 10) element = wait.until(EC.presence_of_element_located((By.ID, 'result'))) This waits up to 10 seconds for the element with ID 'result' to appear.
Result
The script waits properly, avoiding errors from trying to use elements too soon.
Handling waits prevents flaky tests caused by timing issues on dynamic pages.
7
ExpertStructuring scripts for maintainability
🤔Before reading on: do you think writing all test steps in one script is best, or should tests be organized for reuse and clarity? Commit to your answer.
Concept: Organize Selenium scripts using functions, classes, and test frameworks to keep tests clear and easy to update.
Instead of one long script, split tests into functions or classes. Use frameworks like pytest to run tests and report results. Example: import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_example(browser): browser.get('https://example.com') assert 'Example' in browser.title This structure helps run many tests cleanly and share setup code.
Result
Tests become easier to read, maintain, and run automatically in teams.
Good structure is crucial for scaling tests and working in professional teams.
Under the Hood
Selenium works by sending commands from your script to a browser driver, which then controls the browser using its automation interface. The driver translates commands like 'open URL' or 'click element' into browser actions. The browser executes these actions and returns results or errors back to the script. This communication happens over a WebDriver protocol using HTTP requests.
Why designed this way?
Selenium was designed to separate test code from browser control, allowing tests to run on many browsers without changing code. Using a driver for each browser respects browser security and architecture. The WebDriver protocol standardizes commands so tools and browsers can evolve independently.
┌───────────────┐       HTTP       ┌───────────────┐
│ Selenium Test │ ───────────────> │ Browser Driver│
│    Script     │ <────────────── │               │
└───────────────┘                  └──────┬────────┘
                                         │
                                         ▼
                                ┌─────────────────┐
                                │   Web Browser   │
                                └─────────────────┘
Myth Busters - 3 Common Misconceptions
Quick: Does Selenium automatically wait for all page elements to load before acting? Commit yes or no.
Common Belief:Selenium waits automatically for all elements to load before performing actions.
Tap to reveal reality
Reality:Selenium does not wait automatically; you must add explicit or implicit waits to handle dynamic content.
Why it matters:Without waits, tests often fail because elements are not ready, causing flaky and unreliable test results.
Quick: Can Selenium test websites without a real browser? Commit yes or no.
Common Belief:Selenium can test websites without opening a real browser window.
Tap to reveal reality
Reality:Selenium controls real browsers; headless mode runs browsers without a visible window but still uses the full browser engine.
Why it matters:Thinking Selenium works without a browser leads to confusion; headless mode is still a real browser, ensuring accurate tests.
Quick: Is it best to write all test steps in one big script? Commit yes or no.
Common Belief:Writing one big script with all test steps is simpler and better.
Tap to reveal reality
Reality:Organizing tests into smaller, reusable parts improves clarity, maintenance, and scalability.
Why it matters:Large scripts become hard to update and debug, slowing down team work and increasing errors.
Expert Zone
1
Tests can fail silently if element locators are brittle; using stable attributes like data-test-id improves reliability.
2
Implicit waits can cause unpredictable delays; explicit waits give precise control over timing and conditions.
3
Running tests in parallel requires careful browser session management to avoid conflicts and speed up test suites.
When NOT to use
Selenium is not ideal for testing non-web applications or APIs; use tools like Postman for APIs or Appium for mobile apps instead.
Production Patterns
In real projects, Selenium tests are integrated into CI/CD pipelines to run automatically on code changes. Tests use page object models to separate page structure from test logic, improving maintainability.
Connections
API Testing
Complementary testing approach
Knowing Selenium helps understand UI testing, while API testing focuses on backend logic; combining both gives full coverage.
Robotic Process Automation (RPA)
Similar automation pattern
Both Selenium and RPA automate user actions, but RPA targets business workflows beyond testing, showing automation's broad power.
Human-Computer Interaction (HCI)
User behavior simulation
Selenium scripts mimic how users interact with interfaces, linking software testing to understanding user experience in HCI.
Common Pitfalls
#1Trying to find elements before the page fully loads causes errors.
Wrong approach:element = browser.find_element('id', 'dynamic') # immediately after get()
Correct approach:from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By wait = WebDriverWait(browser, 10) element = wait.until(EC.presence_of_element_located((By.ID, 'dynamic')))
Root cause:Misunderstanding that web pages load asynchronously and elements may not be present immediately.
#2Hardcoding element locators that change frequently leads to broken tests.
Wrong approach:button = browser.find_element('xpath', '//div[3]/button[1]')
Correct approach:button = browser.find_element('css selector', '[data-test-id="submit-button"]')
Root cause:Using fragile locators tied to page layout instead of stable, semantic attributes.
#3Not closing the browser after tests wastes resources and causes errors.
Wrong approach:browser = webdriver.Chrome() browser.get('https://example.com') # no browser.quit() call
Correct approach:browser = webdriver.Chrome() browser.get('https://example.com') browser.quit()
Root cause:Forgetting cleanup steps due to lack of test structure or automation framework.
Key Takeaways
Selenium scripts automate browsers by sending commands to control web pages step-by-step.
Setting up the browser driver correctly is essential for Selenium to work.
Locating page elements precisely and waiting for them to load prevents common test failures.
Assertions in scripts turn actions into tests by verifying expected results.
Organizing tests with functions and frameworks improves maintainability and scalability.