Selenium
A framework for automated browser control: functional testing, UI automation, web interaction, scraping-like tasks (within legal/ethical limits).
WebDriver
A Python object that controls the browser.
Examples:
- webdriver.Firefox()
- webdriver.Chrome()
Driver Executable (geckodriver, chromedriver)
A small binary that sits between Selenium and the browser.
- Required for automation.
- Modern Selenium (4.6+) can download it via Selenium Manager automatically.
- Or you can use webdriver-manager, which installs the driver in your user directory, with no sudo needed.
Locators
Methods for identifying elements in the DOM.
Common locator strategies:
- By.ID
- By.NAME
- By.CLASS_NAME
- By.CSS_SELECTOR
- By.XPATH
Interacting with Elements
Typical operations:
- .click()
- .send_keys("text")
- .text
- .get_attribute("href")
Waits
Because modern websites load dynamically, timing matters.
Implicit Wait: Driver waits up to N seconds for each search.
| driver.implicitly_wait(10)
|
Explicit Wait: Wait for a specific condition (recommended).
| WebDriverWait(driver, 10).until(...)
|
Page Object Model (POM) — Concept Only
A design pattern:
- Each page is represented by a Python class.
- Contains locators + methods that perform actions on that page.
- Makes test code cleaner and easier to maintain.
Selenium mini example
Create a virtual environment:
| python3 -m venv ~/venvs/selenium-env
source ~/venvs/selenium-env/bin/activate
|
Install required packages (user-level only):
| pip install --upgrade pip
pip install selenium webdriver-manager
|
No sudo → the driver binaries will be stored under your user folder.
The next example:
- Opens Firefox/Chrome
- Navigates to DuckDuckGo
- Performs a search
- Prints the title + URL of the first result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 | from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.firefox import GeckoDriverManager
# For Chrome use:
# from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.firefox.service import Service as FirefoxService
# For Chrome:
# from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def main():
# --- 1. Driver setup (downloaded into user directory) ---
service = FirefoxService(GeckoDriverManager().install())
# Chrome:
# service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Firefox(service=service)
# Chrome:
# driver = webdriver.Chrome(service=service)
try:
# --- 2. Navigate to page ---
driver.get("https://duckduckgo.com")
# --- 3. Locate search box ---
search_box = driver.find_element(By.NAME, "q")
# --- 4. Perform search ---
search_box.send_keys("Selenium Python tutorial")
search_box.send_keys(Keys.RETURN)
# --- 5. Explicit wait for results ---
wait = WebDriverWait(driver, 10)
first_result = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, "a.result__a"))
)
# --- 6. Output result ---
print("First result title:", first_result.text)
print("First result link:", first_result.get_attribute("href"))
finally:
driver.quit()
if __name__ == "__main__":
main()
|
Selenium + Cucumber
Project structure:
| selenium-cucumber-demo/
│
├── features/
│ ├── search.feature
│ └── steps/
│ └── search_steps.py
│
└── pages/
└── search_page.py
|
Install Dependencies (no sudo)
| python3 -m venv ~/venvs/behave-env
source ~/venvs/behave-env/bin/activate
|
| pip install behave selenium webdriver-manager
|
Create: features/search.feature
| Feature: DuckDuckGo Search
As a user
I want to search for information
So that I can see the first result
Scenario: Search for a term
Given I open DuckDuckGo
When I search for "Selenium Python tutorial"
Then the first result should be visible
|
This defines the behavior in plain English (Cucumber style).
Create: pages/search_page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class DuckDuckGoPage:
URL = "https://duckduckgo.com"
SEARCH_BOX = (By.NAME, "q")
FIRST_RESULT = (By.CSS_SELECTOR, "a.result__a")
def __init__(self, driver):
self.driver = driver
def open(self):
self.driver.get(self.URL)
def search(self, text):
box = self.driver.find_element(*self.SEARCH_BOX)
box.send_keys(text)
box.send_keys(Keys.RETURN)
def wait_for_first_result(self, timeout=10):
wait = WebDriverWait(self.driver, timeout)
return wait.until(
EC.presence_of_element_located(self.FIRST_RESULT)
)
|
Create: features/steps/search_steps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 | from behave import given, when, then
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
# For Chrome:
# from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.firefox.service import Service as FirefoxService
from pages.search_page import DuckDuckGoPage
@given("I open DuckDuckGo")
def step_open_duckduckgo(context):
service = FirefoxService(GeckoDriverManager().install()) # no sudo
context.driver = webdriver.Firefox(service=service)
context.page = DuckDuckGoPage(context.driver)
context.page.open()
@when('I search for "{query}"')
def step_search(context, query):
context.page.search(query)
@then("the first result should be visible")
def step_first_result_visible(context):
result = context.page.wait_for_first_result()
assert result is not None
print("First result:", result.text)
context.driver.quit()
|
This code:
- Starts Firefox via WebDriverManager (no sudo)
- Opens DuckDuckGo
- Performs a search
- Waits for the first result
- Asserts that the result exists and prints it
- Closes the browser
Running Cucumber Tests:
Utolsó frissítés:
2025-11-30 17:10:14