Fill a sign-in form from code
By default, a session with credentials fills a sign-in form as soon as the page shows one, and submits it. To decide the moment yourself, send the Steel.credentials commands over the session's Chrome DevTools Protocol (CDP) connection. Your code says when to fill, which credential to use and whether to submit. It never gets the values from a command: a command returns only what happened to each field. Code that can read the page can still read a field after Steel fills it.
Use the commands when an agent must check the page before it signs in, when a page matches more than one credential, or when a sign-in has several steps.
Before you begin
- A credential that the session can use. See Credentials.
- The Steel SDK and Playwright, with your API key in the
STEEL_API_KEYenvironment variable. See Before you begin on the Credentials page.
The examples use Playwright in TypeScript and Python. The commands work with any CDP client.
Fill and submit a form
Create the session with credentials. Open a CDP session on the browser, and turn off autofill so that Steel waits for your command. Then go to the sign-in page and send Steel.credentials.fill.
A command acts on the browser's active tab. This program fills and submits the form of Steel's practice site with the credential that Store a credential saved.
import Steel from "steel-sdk";
import { chromium } from "playwright";
const client = new Steel();
const session = await client.sessions.create({ credentials: {} });
try {
const browser = await chromium.connectOverCDP(session.websocketUrl);
const context = browser.contexts()[0];
const page = context.pages()[0] ?? (await context.newPage());
// Playwright's types do not know the Steel commands.
const cdp = await browser.newBrowserCDPSession();
const send = (method: string, params: object = {}) => (cdp as any).send(method, params);
await send("Steel.credentials.setControls", { autofill: false });
await page.goto("https://steel.dev/practice/login");
await page.waitForSelector('input[type="password"]');
const result = await send("Steel.credentials.fill", { submit: true });
console.log(result.filled, result.submitted);
await page.waitForURL("https://steel.dev/practice/secure");
console.log(page.url());
await browser.close();
} finally {
await client.sessions.release(session.id);
}from playwright.sync_api import sync_playwright
from steel import Steel
client = Steel()
session = client.sessions.create(credentials={})
try:
with sync_playwright() as playwright:
browser = playwright.chromium.connect_over_cdp(session.websocket_url)
context = browser.contexts[0]
page = context.pages[0] if context.pages else context.new_page()
cdp = browser.new_browser_cdp_session()
cdp.send("Steel.credentials.setControls", {"autofill": False})
page.goto("https://steel.dev/practice/login")
page.wait_for_selector('input[type="password"]')
result = cdp.send("Steel.credentials.fill", {"submit": True})
print(result["filled"], result["submitted"])
page.wait_for_url("https://steel.dev/practice/secure")
print(page.url)
browser.close()
finally:
client.sessions.release(session.id)The result of fill has filled, the list of the parts that Steel filled (username, password or otp), and submitted. It also has one entry in fields for each field with its outcome, such as filled or not_empty. Steel never writes over a field that already has text.
Without submit, fill leaves the form filled and does not submit it. Submit it later with Steel.credentials.submit, or click the button yourself.
A filled field does not prove that the site accepted the login. Wait for a page or an element that only a signed-in user sees.
Fill first, submit later
To check the page between the fill and the submit, send the two commands yourself. Steel.credentials.detect returns the frames of the active tab, each with its frameId, its documentId and the groups of fields that make up its sign-in forms. Pass the same three IDs to fill and to submit, so that both act on one form.
const detected = await send("Steel.credentials.detect");
const frame = detected.frames.find((f: { groups: unknown[] }) => f.groups.length > 0);
const form = {
frameId: frame.frameId,
documentId: frame.documentId,
groupId: frame.groups[0].groupId,
};
const filled = await send("Steel.credentials.fill", form);
console.log(filled.filled);
await send("Steel.credentials.submit", form);Put your own check between the two calls. submit does the same origin checks as fill, and it fails when a field of the form is empty or not valid.
Choose between credentials
When more than one credential matches the page, fill fails with credential_ambiguous and writes nothing. Ask Steel which credentials match, then name one.
Steel.credentials.detect with checkCredentials: true returns the sign-in forms of the active tab. Each frame has its credentialCandidates: the id, name, origin and reference of each credential that matches, without values.
const detected = await send("Steel.credentials.detect", { checkCredentials: true });
const candidates = detected.frames[0].credentialCandidates;
const billing = candidates.find((candidate: { name: string }) => candidate.name === "billing");
await send("Steel.credentials.fill", { credentialId: billing.id, submit: true });fill takes a credentialId or a reference, not both. A reference names one item, such as steel://vaults/acme-prod/billing. Neither gives the session new access: the credential must already be one that the session can use on this page.
Commands
| Command | What it does |
|---|---|
Steel. | Lists the sign-in forms of the active tab, by frame. With checkCredentials: true, also lists the credentials that match. |
Steel. | Fills one sign-in form. With submit: true, also submits it. |
Steel. | Submits a form that is already filled. Needs the frameId, documentId and groupId that detect or fill returned. |
Steel. | Returns the session's controls. |
Steel. | Changes one or more controls. |
Steel. | Makes the open forms check their credentials again, for example after you change the session's bindings. |
Steel. | Returns what Steel did in the tab: detected, filled, submitted, blocked and navigated events. With waitMs, waits up to 30 seconds for the next one. |
When a page has more than one sign-in form, fill fails with login_ambiguous. Call detect, then pass the frameId, documentId and groupId of the form that you want.
Controls
A control turns one part of credential filling on or off for the whole session. setControls takes the controls that you want to change.
| Control | When it is on |
|---|---|
resolution | The session can use credentials. Turn it off to stop every fill, also one from a command. |
autofill | Steel fills a form when the page shows it. |
autoSubmit | Steel submits a form after it fills the form on its own. A command's submit does not depend on it. |
blurFields | Steel hides the text of the fields that it fills from view. The values stay in the fields, where a script in the page can read them. |
domTrigger | A script in the page can ask Steel to fill or submit. It is off by default. |
autoSubmit, blurFields and domTrigger start with the values of the session's credentials options.
One-time passwords
When the form has a one-time password field and the credential has a one-time password secret, fill asks Steel for a code and fills it. Steel makes the code on its servers. The secret never reaches the browser.
A code is valid for a short time. If it expires before the form is submitted, the submit fails with submit_unavailable and the reason otp_expired. Clear the field, then call fill again.
Check that it worked
Run the program in Fill and submit a form:
npx tsx fill-form.mtspython fill_form.pyThe program prints the parts that Steel filled and true for the submit. Then it prints https://steel.dev/practice/secure, the URL of the page that a signed-in user sees.
Troubleshooting
A command that fails returns a CDP error. Its code is in error.data.code. Playwright does not pass data on: its error has only the message, such as Protocol error (Steel.credentials.fill): credential ambiguous. To read a code or the candidates in a Playwright program, call detect with checkCredentials: true before the fill.
no_login_detected
Cause: Steel found no sign-in form in the active tab. The form is not there yet, or another tab is active.
Fix: Wait for the form's password field before you call fill. Bring the tab to the front.
Verify: Steel.credentials.detect returns a frame with a group.
credential_origin_not_found
Cause: No credential that the session can use matches the page's origin. A fill with a credentialId or a reference that is not bound to the session returns the same code. So does a binding that you removed, or one whose Secret Connection was deleted.
Fix: List the session's bindings with GET /v1/sessions/$SESSION_ID/credentials, and compare the origin of each credential with the page's scheme, host and port. The response does not list the credentials of a bound Vault: for those, call GET /v1/secrets?kind=credential&vaultId=$VAULT_ID. See The sign-in form stays empty.
Verify: detect with checkCredentials: true returns credentialAvailable: true for the frame.
credential_ambiguous
Cause: More than one credential matches the page.
Fix: Pass a credentialId or a reference, as Choose between credentials shows. The error has the candidates in error.data.candidates. In Playwright, get them from detect with checkCredentials: true.
Verify: fill returns the filled parts in filled.
credentials_disabled
Cause: The resolution control is off.
Fix: Send Steel.credentials.setControls with resolution: true.
Verify: Steel.credentials.getControls returns resolution: true in controls.