In the last lesson, we learned **what Selenium is**.
Now let's understand what happens behind the scenes when Selenium automates a browser. π€π
## π§ The Basic Idea
When you write Selenium code, your program doesn't directly control Chrome or Firefox.
Instead, Selenium communicates with the browser through a **WebDriver**.
Think of it like this:
**Your Test Code β Selenium WebDriver β Browser β Web Page**
For example:
```text
Python Test
β
Selenium
β
ChromeDriver
β
Google Chrome
β
Web Application
```
Selenium sends instructions such as:
π Open this URL
π Find this button
π Enter this username
π Click Login
π Verify the dashboard
The browser then performs those actions.
---
## π Step 1: Your Test Code
You first write a test using a programming language such as Python, Java, or JavaScript.
Example:
```python
```
This tells Selenium:
> "Open this website."
---
## π Step 2: Selenium Sends the Command
Selenium takes your instruction and communicates with the appropriate browser automation interface.
For example:
```text
driver.get()
β
Selenium
β
Browser automation interface
```
---
## π Step 3: The Browser Executes It
The browser receives the command and performs the requested action.
For example:
```text
Open URL
β
Chrome navigates to the website
β
Page loads
```
---
## π― Step 4: Selenium Finds Elements
Suppose your login page contains:
```html
<input id="username">
```
You can locate it using Selenium:
```python
driver.find_element(By.ID, "username")
```
Selenium identifies the element in the page and allows your test to interact with it.
For example:
```python
driver.find_element(By.ID, "username").send_keys("testuser")
```
Now Selenium enters:
**testuser**
into the username field.
---
## π±οΈ Step 5: Selenium Performs Actions
You can perform actions such as:
```text
Click
Type
Clear
Select
Scroll
Submit
Navigate
```
Example:
```python
driver.find_element(By.ID, "login").click()
```
Selenium finds the login button and clicks it.
---
## β
Step 6: Selenium Verifies the Result
Automation isn't useful just because it can click buttons.
A QA Engineer needs to **verify the expected result**.
For example:
```python
assert "Dashboard" in driver.title
```
The test checks whether the expected dashboard was reached.
So the complete flow becomes:
```text
Write Test
β
Selenium
β
WebDriver
β
Browser
β
Find Element
β
Perform Action
β
Application Responds
β
Verify Result
β
PASS / FAIL
```
---
# π§© Real-World Example: Login Test
Imagine you want to automate this:
```text
Open Website
β
Enter Username
β
Enter Password
β
Click Login
β
Verify Dashboard
```
Your Selenium test could look conceptually like:
```python
driver.find_element(By.ID, "username").send_keys("testuser")
driver.find_element(By.ID, "password").send_keys("password")
driver.find_element(By.ID, "login").click()
assert "Dashboard" in driver.title
```
Selenium is essentially turning the manual test steps into **repeatable automated instructions**.
---
# π₯ Why Is WebDriver Important?
The **WebDriver** is the bridge between your automation code and the browser.
Without this communication layer, your Python/Java/JavaScript code wouldn't simply be able to tell Chrome:
> "Click this button."
WebDriver enables Selenium to interact with the browser in a standardized way.
---
# π§ QA Tip
Understanding Selenium isn't just about memorizing:
```python
find_element()
click()
send_keys()
```
A good QA Engineer should understand the complete flow:
**Test β Locate β Act β Observe β Verify**
That's the foundation of browser automation.
---
## π¬ Challenge for the Community
Imagine you have this login flow:
```text
Username
Password
Login Button
Dashboard
```
### Question:
What would you need to do in Selenium to automate this test?
Try to write the steps in your own words.
π Comment your answer below.
**Bonus:** Which locator would you use to find the username field?
`ID` | `Name` | `XPath` | `CSS Selector`
Let's see what everyone chooses! π