How to Press Enter in Selenium Without a WebElement in Python
Use Selenium Python’s ActionChains to send Enter to the currently focused element without passing a WebElement. Focus determines where the key goes.
Use ActionChains to press Enter on whichever page element currently has focus, without passing a WebElement to the key action:
from selenium.webdriver import ActionChains, Keys
ActionChains(driver).send_keys(Keys.ENTER).perform()
This sends the key to the current focused element. Selenium does not choose a target for you: first make sure the intended input, button, or other keyboard-interactable control has focus. See the Selenium ActionChains API.
Use ActionChains when focus is already correct
ActionChains(driver).send_keys(Keys.ENTER).perform() is the direct answer when you do not want to pass a WebElement to the Enter action. It is useful when a previous page interaction established focus, or when the application has already focused a control.
For example, if a search field is already focused by the page, send Enter directly:
from selenium.webdriver import ActionChains, Keys
ActionChains(driver).send_keys(Keys.ENTER).perform()
The call has three parts:
ActionChains(driver)creates an action sequence for the WebDriver session.send_keys(Keys.ENTER)queues an Enter key input for the currently focused element.perform()executes the queued action.
Complete runnable example with focus established
This example starts a browser, opens a page with a search field, focuses that field, types a query, and sends Enter without supplying a WebElement to the Enter action. Install Selenium and a compatible browser driver before running it:
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
def main():
driver = webdriver.Chrome()
try:
driver.get("https://www.selenium.dev/")
# Locate and focus the intended control first.
search_box = driver.find_element(By.NAME, "q")
search_box.click()
search_box.send_keys("WebDriver")
# Enter is sent to the currently focused element, with no WebElement argument.
ActionChains(driver).send_keys(Keys.ENTER).perform()
# Wait for or inspect the page state your application expects here.
print("Current URL:", driver.current_url)
finally:
driver.quit()
if __name__ == "__main__":
main()
The field is used to establish focus and enter text. The Enter action itself is sent through ActionChains without a target element. The example’s result depends on the page’s own keyboard and form behavior; Selenium’s key action does not guarantee a particular navigation or submission.
Choose the right key method
| Situation | Use | Why |
|---|---|---|
| The intended control already has focus, and you do not want to pass a WebElement | ActionChains(driver).send_keys(Keys.ENTER).perform() |
Sends keys to the current focus. |
| You have located the target input or keyboard-interactable element | element.send_keys(Keys.RETURN) |
Sends keys directly to that element. |
| You need a key-down as part of a longer action sequence | ActionChains(driver).key_down(Keys.ENTER).perform() |
key_down accepts an optional element; when omitted, the current focus is targeted. For an ordinary Enter press, send_keys is simpler. |
Selenium’s element interaction documentation describes element-directed key input for text fields and keyboard-interactable elements. If the target is known, this direct form is often clearer:
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
element = driver.find_element(By.NAME, "q")
element.send_keys("WebDriver")
element.send_keys(Keys.RETURN)
The Selenium Python bindings guide demonstrates the element-directed Keys.RETURN pattern. The focused ActionChains form is the choice when the key action should not receive a WebElement argument.
Focus and key details
Focus is the target
ActionChains does not search for a suitable input or infer which control you mean. The active element may be an input, a button, a page-level control, or nothing useful. A click, tab navigation, or an application action may set focus; verify that it is the intended recipient before sending Enter.
ENTER and RETURN
Selenium exposes both Keys.ENTER and Keys.RETURN. Use one naming convention consistently in a codebase. The API example here uses Keys.ENTER; the bindings guide also shows Keys.RETURN for element-directed input.
Enter is page input, not a browser-chrome shortcut
This method sends keyboard input to the page’s focused element. It is not a documented way to invoke browser menus, address-bar behavior, or other browser-chrome shortcuts.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Enter appears to do nothing | No suitable element has focus, or the focused control does not handle Enter. | Click or otherwise focus the intended control first. Check the active page state and the application’s expected keyboard behavior. |
| The wrong field or control receives Enter | Focus moved after a prior click, tab, popup, or rerender. | Re-establish focus immediately before sending the key; inspect the active element if needed. |
send_keys runs but the expected form is not submitted |
The page may require a different control, validation state, or explicit submit interaction. | Confirm the control’s actual behavior. If it is a form, check required fields and application validation; use the page’s intended submit control when appropriate. |
Python reports that ActionChains or Keys is undefined |
The Selenium imports are missing or incorrect. | Use from selenium.webdriver import ActionChains, Keys, or import Keys from selenium.webdriver.common.keys. |
| The browser cannot start before the key action | The browser or driver setup is not available or compatible. | Resolve the local Selenium, browser, and driver setup first; the keyboard action cannot run until a WebDriver session exists. |
| A JavaScript-triggered submit behaves differently | Executing JavaScript is a different operation from sending keyboard input and does not establish that the page received an Enter key event. | Use the WebDriver key action when the behavior under test is keyboard input. |
Reliability and test design
- Make focus an explicit precondition in the test. If a prior action is expected to focus the control, assert or otherwise inspect the resulting page state where practical.
- Send the key after focus is established, close to the action that depends on it. This reduces confusion when intervening page behavior changes focus.
- Assert the outcome your application promises, such as a result state or navigation, rather than assuming every Enter press submits a form.
- Prefer element-directed
send_keyswhen a locator is available and the test is specifically about that element. Use focused ActionChains input when the focused target is the behavior you need to exercise.
Or skip the browser setup
If your goal is to capture a page after an interaction, Selenium requires browser and driver setup. ScreenshotNeo is a website screenshot API and MCP server; one GET request captures a URL as PNG, JPEG, WebP, or PDF. It does not replace Selenium for testing a page’s Enter-key behavior, but it can skip local browser setup when you need a page capture. See the ScreenshotNeo website and API documentation.
Example cURL request:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Python:
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
timeout=90,
)
open("shot.webp", "wb").write(r.content)
Node.js:
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
ScreenshotNeo accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before the shot; each of those steps can be turned off. Bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents use take_screenshot, get_page_info, and capture_pdf. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Only clean shots are billed, and response headers identify the page verdict and billing status. Every feature is available on every plan.
Sign up for 1,000 free screenshots a month, with no card required.
FAQ
How do I press Enter in Selenium without a WebElement?
Call ActionChains(driver).send_keys(Keys.ENTER).perform(). It sends Enter to the currently focused page element.
Does ActionChains send Enter to the element under the mouse?
No. The documented target is the currently focused element. Hover position does not establish the keyboard recipient.
Can I use this to press Enter in the browser address bar?
This is a WebDriver page-input action. The cited Selenium API does not describe it as a way to control browser chrome.
Should I use ENTER or RETURN?
Both Selenium key constants are available. Keep the chosen constant consistent; the examples use ENTER for ActionChains and show the documented RETURN element pattern.


