πŸ“˜ Chapter 3: Selenium WebDriver – Core Concepts


πŸ”Ή 3.1 Working with Web Elements

βœ… find_element() and find_elements()

These are the most commonly used Selenium methods for locating HTML elements.

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://example.com")

# Single element
username_input = driver.find_element(By.ID, "username")

# Multiple elements
all_links = driver.find_elements(By.TAG_NAME, "a")

πŸ”Ή Why needed?

You can’t interact with a page unless you can locate the HTML element.

βœ… click(), send_keys(), text, get_attribute()

username_input.send_keys("admin")
login_button = driver.find_element(By.ID, "submit")
login_button.click()

πŸ”Ή 3.2 Handling Radio Buttons and Checkboxes

click() to select:

radio = driver.find_element(By.ID, "genderMale")
radio.click()

Check if selected:

is_selected = radio.is_selected()

πŸ”Ή 3.3 Handling Dropdowns

Use the Select class.

from selenium.webdriver.support.ui import Select

dropdown = Select(driver.find_element(By.ID, "country"))
dropdown.select_by_visible_text("India")

πŸ”Ή 3.4 Handling Alerts & Popups

alert = driver.switch_to.alert
print(alert.text)
alert.accept()  # or alert.dismiss()

πŸ”Ή 3.5 Handling Windows and Tabs

original = driver.current_window_handle
all_windows = driver.window_handles

driver.switch_to.window(all_windows[1])  # Go to new tab
driver.switch_to.window(original)        # Come back

πŸ”Ή 3.6 Handling Frames and iFrames

driver.switch_to.frame("frameName")
# Interact inside the frame
driver.switch_to.default_content()  # Exit frame

πŸ”Ή 3.7 Browser Navigation

driver.get("https://google.com")
driver.back()
driver.forward()
driver.refresh()

πŸ”Ή 3.8 Get Current URL and Title

print(driver.current_url)
print(driver.title)

πŸ”Ή 3.9 Waits: Implicit & Explicit

βœ… Implicit Wait

driver.implicitly_wait(10)

βœ… Explicit Wait

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "username")))

πŸ”Ή 3.10 JavaScript Execution

driver.execute_script("alert('Hello from JS!')")

Used when:

πŸ”Ή 3.11 Taking Screenshots

driver.save_screenshot("screenshot.png")

πŸ”Ή 3.12 Working with ActionChains

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
element = driver.find_element(By.ID, "hoverMenu")

actions.move_to_element(element).perform()       # Hover
actions.context_click(element).perform()         # Right click

# Drag and Drop
source = driver.find_element(By.ID, "dragMe")
target = driver.find_element(By.ID, "dropHere")
actions.drag_and_drop(source, target).perform()

πŸ”Ή 3.13 Working with Cookies

# Get cookies
print(driver.get_cookies())

# Add cookie
driver.add_cookie({'name': 'mycookie', 'value': '123456'})

# Delete cookies
driver.delete_all_cookies()

🧠 Summary

Topic Key Methods
Web Elementsfind_element, click(), send_keys()
DropdownsSelect class
Alertsswitch_to.alert
Windows/Tabsswitch_to.window()
Framesswitch_to.frame()
Waitsimplicitly_wait(), WebDriverWait
JavaScriptexecute_script()
Screenshotssave_screenshot()
ActionChainsmove_to_element(), context_click(), drag_and_drop()
Cookiesget_cookies(), add_cookie(), delete_all_cookies()