Selenium 4 (checked against 4.54). driver is any browser driver, for example
driver = webdriver.Chrome().
Selenium 4.3 removed every find_element_by_* helper. Use find_element /
find_elements with a By locator.
from selenium.webdriver.common.by import By
| Locator |
Finds by |
Usage |
ID |
id attribute |
driver.find_element(By.ID, "foo") |
NAME |
name attribute |
driver.find_element(By.NAME, "q") |
CLASS_NAME |
one CSS class |
driver.find_element(By.CLASS_NAME, "btn") |
TAG_NAME |
tag name |
driver.find_elements(By.TAG_NAME, "a") |
CSS_SELECTOR |
CSS selector |
driver.find_element(By.CSS_SELECTOR, "#login > button") |
XPATH |
XPath |
driver.find_element(By.XPATH, "//div/td[1]") |
LINK_TEXT |
exact link text |
driver.find_element(By.LINK_TEXT, "Sign In") |
PARTIAL_LINK_TEXT |
part of the link text |
driver.find_element(By.PARTIAL_LINK_TEXT, "Sign") |
find_element raises NoSuchElementException when nothing matches;
find_elements returns an empty list.
from selenium.webdriver.support.relative_locator import locate_with
| Method |
Finds the element |
Usage |
above |
above another element |
driver.find_element(locate_with(By.TAG_NAME, "input").above(password)) |
below |
below another element |
locate_with(By.TAG_NAME, "input").below(email) |
to_left_of |
left of another element |
locate_with(By.TAG_NAME, "button").to_left_of(submit) |
to_right_of |
right of another element |
locate_with(By.TAG_NAME, "button").to_right_of(cancel) |
near |
within 50 px (or a given distance) |
locate_with(By.TAG_NAME, "label").near(email) |
| Method |
Description |
Usage |
get |
Loads a page in the current window. |
driver.get("https://example.com") |
title |
Title of the current page. |
driver.title |
current_url |
URL of the current page. |
driver.current_url |
page_source |
HTML source of the current page. |
driver.page_source |
name |
Name of the browser, e.g. chrome. |
driver.name |
capabilities |
Capabilities of the running session (replaces desired_capabilities). |
driver.capabilities["browserVersion"] |
find_element |
First element matching a locator. |
driver.find_element(By.ID, "foo") |
find_elements |
All elements matching a locator. |
driver.find_elements(By.CSS_SELECTOR, "li") |
execute_script |
Runs JavaScript synchronously and returns its result. |
driver.execute_script("return document.title") |
execute_async_script |
Runs JavaScript that calls the callback passed as its last argument. |
driver.execute_async_script("arguments[0]('done')") |
print_page |
Prints the page to PDF, returned as base64. |
driver.print_page() |
close |
Closes the current window. |
driver.close() |
quit |
Ends the session and closes every window. |
driver.quit() |
| Method |
Description |
Usage |
current_window_handle |
Handle of the current window. |
driver.current_window_handle |
window_handles |
Handles of all open windows. |
driver.window_handles |
maximize_window |
Maximises the window. |
driver.maximize_window() |
minimize_window |
Minimises the window. |
driver.minimize_window() |
fullscreen_window |
Makes the window full screen. |
driver.fullscreen_window() |
get_window_rect |
Position and size as a dict. |
driver.get_window_rect() |
set_window_rect |
Sets position and/or size. |
driver.set_window_rect(x=0, y=0, width=1280, height=800) |
get_window_size |
Width and height. |
driver.get_window_size() |
set_window_size |
Sets width and height. |
driver.set_window_size(1280, 800) |
The Selenium 3 switch_to_* methods are gone; use driver.switch_to.
| Method |
Description |
Usage |
window |
Switches to a window by handle or name. |
driver.switch_to.window(handle) |
new_window |
Opens a new "tab" or "window" and switches to it. |
driver.switch_to.new_window("tab") |
frame |
Switches to a frame by index, name or element. |
driver.switch_to.frame("frame_name") |
parent_frame |
Switches to the parent frame. |
driver.switch_to.parent_frame() |
default_content |
Switches back to the top-level document. |
driver.switch_to.default_content() |
active_element |
Element that has focus. |
driver.switch_to.active_element |
alert |
The open alert, see alerts. |
driver.switch_to.alert |
| Method |
Description |
Usage |
back |
One step back in history. |
driver.back() |
forward |
One step forward in history. |
driver.forward() |
refresh |
Reloads the page. |
driver.refresh() |
| Method |
Description |
Usage |
get_cookies |
All cookies visible to the page, as dicts. |
driver.get_cookies() |
get_cookie |
One cookie by name, or None. |
driver.get_cookie("my_cookie") |
add_cookie |
Adds a cookie; name and value are required. |
driver.add_cookie({"name": "foo", "value": "bar"}) |
delete_cookie |
Deletes a cookie by name. |
driver.delete_cookie("my_cookie") |
delete_all_cookies |
Deletes every cookie in scope. |
driver.delete_all_cookies() |
| Method |
Description |
Usage |
implicitly_wait |
Sticky wait for elements to appear on every find. Prefer explicit waits. |
driver.implicitly_wait(5) |
set_page_load_timeout |
Maximum time for a page load. |
driver.set_page_load_timeout(30) |
set_script_timeout |
Maximum time for execute_async_script. |
driver.set_script_timeout(30) |
timeouts |
Read or set all three at once. |
driver.timeouts.page_load |
| Method |
Description |
Usage |
save_screenshot |
Saves a PNG of the window; returns False on I/O errors. |
driver.save_screenshot("/tmp/page.png") |
get_screenshot_as_file |
Same as save_screenshot. |
driver.get_screenshot_as_file("/tmp/page.png") |
get_screenshot_as_png |
PNG bytes. |
driver.get_screenshot_as_png() |
get_screenshot_as_base64 |
Base64 string, handy for HTML reports. |
driver.get_screenshot_as_base64() |
| Selenium 3 |
Selenium 4 |
driver.find_element_by_id("foo") and the other find_element(s)_by_* |
driver.find_element(By.ID, "foo") |
driver.switch_to_window(h), switch_to_frame, switch_to_alert, switch_to_default_content, switch_to_active_element |
driver.switch_to.window(h), .frame(), .alert, .default_content(), .active_element |
driver.desired_capabilities |
driver.capabilities |
webdriver.Chrome(executable_path=...) |
webdriver.Chrome(service=Service(path)), or nothing: Selenium Manager finds the driver |
webdriver.Chrome(desired_capabilities=..., chrome_options=...) |
webdriver.Chrome(options=webdriver.ChromeOptions()) |
| PhantomJS |
Any browser in headless mode, e.g. options.add_argument("--headless=new") |