Web Driver Methods in Selenium Python
Selenium is a powerful Python library used for automating web browsers. Its WebDriver API allows you to control browsers programmatically interacting with HTML elements, navigating pages, taking screenshots, executing JavaScript, handling cookies and much more.
This guide explains how to create WebDriver objects, configure them and use various WebDriver methods to automate browser tasks effectively.
Creating a WebDriver Object
To begin using Selenium, the first step is to create a WebDriver object. This object acts as an interface to the browser you want to automate. Each supported browser (Chrome, Firefox, Edge, etc.) has its own WebDriver class.
Example: Creating a Firefox WebDriver
from selenium import webdriver
# Create a WebDriver object for Firefox
driver = webdriver.Firefox()
Arguments for WebDriver
When initializing a WebDriver, you can pass optional arguments to customize the browserâs behavior. These options let you control advanced settings such as headless mode, custom profiles, proxy configurations and more.
Argument | Description |
---|---|
desired_capabilities | A dictionary of capabilities to request when starting the browser session. Required for advanced configuration. |
browser_profile | Selenium FirefoxProfile object (only for Firefox). |
proxy | selenium.webdriver.common.proxy.Proxy object to configure browser proxy settings. |
keep_alive | Boolean; whether HTTP connections should use keep-alive. Default is False. |
file_detector | Custom file detector object for file uploads. Default is LocalFileDetector(). |
options | Instance of Options class (browser-specific configuration like headless mode, window size, etc.). |
How to use Webdriver in Selenium ?
Once you have a WebDriver object, you can control the browser to open pages, interact with elements, and perform automated actions using different WebDriver methods.
Example: Open a webpage and execute JavaScript
from selenium import webdriver
# Create WebDriver
driver = webdriver.Firefox()
# Open a webpage
driver.get("https://www.geeksforgeeks.org/")
# Execute JavaScript to create an alert
script = "alert('Hello from Selenium!')"
driver.execute_script(script)
Output:

WebDriver Methods in Selenium Python
Selenium WebDriver provides a wide range of methods to manipulate the browser. Hereâs a categorized overview:
Navigation Methods
These methods allow you to control browser navigation, such as going to a URL, moving back or forward in history, and refreshing the page.
Method | Description |
---|---|
get(url) | Opens the specified URL in the browser. |
back() | Navigates one step back in browser history. |
forward() | Navigates one step forward in browser history. |
refresh() | Refreshes the current page. |
set_page_load_timeout(time) | Sets maximum time to wait for a page to load. |
Window Management Methods
You can control the browser windowâresize it, move it, or retrieve its size and position.
Method | Description |
---|---|
maximize_window() | Maximizes the browser window. |
minimize_window() | Minimizes the browser window. |
fullscreen_window() | Makes the window fullscreen. |
get_window_size() | Returns width and height of the window. |
get_window_position() | Returns x and y position of the window. |
set_window_rect(x, y, width, height) | Sets window position and size. |
set_window_position(x, y) | Moves the window to the specified coordinates. |
get_window_rect() | Returns position and size as a dictionary. |
Element Interaction Methods
These methods let you find elements on a page and interact with them using locators like ID, XPath, CSS_SELECTOR, etc.
Example: Click a button and type text
from selenium.webdriver.common.by import By
# Locate an element by ID
search_box = driver.find_element(By.ID, "search-input")
# Enter text
search_box.send_keys("Selenium Python")
# Submit search
search_box.submit()
Cookie Management Methods
WebDriver allows you to manage cookies to simulate logged-in sessions or test site behavior.
Method | Description |
---|---|
add_cookie(cookie_dict) | Adds a cookie to the current session. |
get_cookie(name) | Gets a cookie by name. |
get_cookies() | Returns all cookies in the current session. |
delete_cookie(name) | Deletes a cookie by name. |
delete_all_cookies() | Deletes all cookies for the session. |
JavaScript Execution Methods
You can run JavaScript on the current page using these methodsâsynchronously or asynchronously.
Method | Description |
---|---|
execute_script(js_code) | Synchronously executes JavaScript. |
execute_async_script(js_code) | Executes JavaScript asynchronously (useful for callbacks). |
Screenshot Methods
These methods help capture browser screenshots as image files, binary data or base64-encoded strings.
Method | Description |
---|---|
get_screenshot_as_file("file.png") | Saves screenshot as PNG file. |
get_screenshot_as_png() | Returns screenshot as binary data. |
get_screenshot_as_base64() | Returns screenshot as base64 string. Useful for embedding in HTML reports. |
Session & Page Info Methods
You can retrieve details about the current session and page using these properties.
Method | Description |
---|---|
current_url | Returns current page URL. |
title | Returns the page title. |
page_source | Returns full HTML source of the page. |
current_window_handle | Returns handle of the current window. |
quit() | Closes all browser windows and ends session. |
close() | Closes the current browser window. |
Timeout Methods
These methods help manage how long WebDriver waits for certain operationsâsuch as finding elements or waiting for scripts.
Method | Description |
---|---|
implicitly_wait(seconds) | Sets a timeout to wait for elements to appear. |
set_script_timeout(seconds) | Maximum wait time for execute_async_script(). |
Advanced Methods
These are more advanced tools for working with logs or manually creating web elements.
Method | Description |
---|---|
create_web_element(element_id) | Creates a WebElement object with the specified ID. |
get_log(log_type) | Retrieves browser logs (console, performance, etc.). |
Example Project Using WebDriver Methods
Below is a simple example that uses various WebDriver methods to open a page, take a screenshot, run JavaScript, and close the browser.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
# WebDriver object
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# Open a webpage
driver.get("https://www.geeksforgeeks.org/")
# Execute JavaScript alert
driver.execute_script("alert('Hello Selenium!')")
# Switch to alert and accept it
alert = driver.switch_to.alert
print("Alert text:", alert.text)
time.sleep(2)
alert.accept()
print("Page Title:", driver.title)
driver.quit()
Output:

DevTools listening on ws://127.0.0.1:57695/devtools/browser/f2b5b6b4-b070-4193-ac98-fdade9dc6a19
Alert text: Hello Selenium!
Page Title: GeeksforGeeks | Your All-in-One Learning Portal
Explanation:
- Initialize WebDriver using ChromeDriverManager to launch Chrome.
- Open webpage and show a JavaScript alert via execute_script().
- Handle the alert by switching to it, printing its text and accepting it.
- Print page title and close the browser using driver.quit().