Advanced Selenium WebDriver Techniques: Frames, Windows, and JavaScript Execution
Frames
Switching context into an iframe requires targeting its identifier or index.
from selenium import webdriver
browser = webdriver.Chrome()
browser.switch_to.frame("frame_identifier")
Window Management
When interactions trigger new browser tabs or windows, the driver remains on the original window. Iterate through available handles to shift focus.
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
browser = webdriver.Chrome()
browser.get("https://example.com")
browser.implicitly_wait(30)
# Trigger a link that opens a new window
browser.find_element(By.XPATH, "//a[@class='open-new-tab']").click()
sleep(2)
primary_window = browser.current_window_handle
print(f"Primary Window: {primary_window}")
for handle in browser.window_handles:
if handle != primary_window:
browser.switch_to.window(handle)
sleep(2)
browser.find_element(By.NAME, "username").send_keys("test_user")
Page Navigation & Properties
Refreshing the current page:
browser.refresh()
Retrieving the active browser driver name:
print(browser.name)
Element Attribute and State Extraction
Fetching a specific attribute value from a element:
search_input = browser.find_element(By.ID, "search-box")
current_value = search_input.get_attribute("value")
print(current_value)
Checking element states:
search_input = browser.find_element(By.ID, "search-box")
# Is the element visible on the page?
is_visible = search_input.is_displayed()
# Is the element enabled for interaction?
is_active = search_input.is_enabled()
# Is a checkbox or radio button selected?
is_checked = search_input.is_selected()
Mouse Action Chains
Complex cursor interactions such as hovering, right-clicking, or double-clicking rely on the ActionChains utility.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
target_element = browser.find_element(By.ID, "hover-item")
# Hovering over an element
ActionChains(browser).move_to_element(target_element).perform()
# Right-click and double-click examples
menu_item = browser.find_element(By.ID, "context-menu")
actions = ActionChains(browser)
actions.context_click(menu_item).perform()
actions.double_click(menu_item).perform()
Select Dropdown Lists
The Select class provides methods to choose options by index, value attribute, or visible text.
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
dropdown_element = browser.find_element(By.ID, "country-list")
dropdown = Select(dropdown_element)
# Select by index (0-based)
dropdown.select_by_index(2)
# Select by the 'value' attribute
dropdown.select_by_value("us")
# Select by the visible text
dropdown.select_by_visible_text("United States")
JavaScript Alerts
Browser-native dialog boxes require switching to the alert context before interacting.
from selenium import webdriver
browser = webdriver.Chrome()
alert_box = browser.switch_to.alert
# Retrieve alert text
print(alert_box.text)
# Accept the alert (OK button)
alert_box.accept()
# Dismiss the alert (Cancel button)
alert_box.dismiss()
# Input text into a prompt alert
alert_box.send_keys("confirmation_text")
Explicit and Implicit Waits
Waiting for elements to reach a specific state ensures synchronization. WebDriverWait combined with expected_conditions offers robust explicit waits.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get("https://example.com")
# Wait until an element is clickable
submit_btn = WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.ID, "submit-btn"))
)
submit_btn.click()
Validating specific text presence or element visibility dynamically:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get("https://example.com/login")
browser.find_element(By.ID, "email_field").send_keys(" ")
sleep(2)
browser.find_element(By.ID, "password_field").send_keys(" ")
sleep(2)
browser.find_element(By.LINK_TEXT, "Sign In").click()
# Wait until specific text appears within an element
text_present = WebDriverWait(browser, 10).until(
EC.text_to_be_present_in_element(
(By.XPATH, "//div[@class='error-msg']"),
"Invalid credentials"
)
)
# Wait until an element becomes visible
error_visible = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "error-msg"))
)
if text_present:
print("Validation passed")
Scrolling the Browser Window
Injecting JavaScript via execute_script allows vertical page scrolling.
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.get("https://example.com/long-article")
# Scroll down
scroll_down_script = "window.scrollTo(0, 1000);"
sleep(3)
browser.execute_script(scroll_down_script)
# Scroll back to the top
scroll_up_script = "window.scrollTo(0, 0);"
sleep(3)
browser.execute_script(scroll_up_script)
Rich Text Editor Manipulation
Injecting content directly into an iframe-based rich text editor bypasses standard WebDriver limitations.
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome()
browser.implicitly_wait(30)
def inject_editor_content(payload):
"""Inserts text into a rich text editor's body."""
js_script = f"document.getElementById('editor_iframe').contentWindow.document.body.innerHTML = '{payload}';"
browser.execute_script(js_script)
browser.get("https://example.com/editor-demo")
inject_editor_content("Automated content insertion")
sleep(3)
Modifying Element Attributes
Removing the readonly attribute from an input field enables direct WebDriver text entry.
def remove_readonly_and_set_value(input_value):
"""Strips the readonly attribute and injects a new value."""
js_script = f""""
var element = document.querySelector("input[placeholder='start_time']");
element.removeAttribute('readonly');
element.value = '{input_value}';
"""
browser.execute_script(js_script)
Capturing Screenshots
Saving the current viewport state as an image file is useful for debugging.
from selenium import webdriver
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.get("https://example.com")
browser.save_screenshot("page_capture.png")
browser.get_screenshot_as_file("full_page_capture.png")