If you're new to browser automation, you're in the right place. Testing Examples is a small, free, open source site built for anyone learning to make a browser do things automatically — click, type, search, wait, assert — whether you've never written a line of code or you already know the ropes and just want a quick reference or somewhere reliable to try something out.

Learn Practice Code

Examples

Examples of source code

Selenium + JavaScript
import { Builder, By, Key } from 'selenium-webdriver';

async function demo() {
  const driver = await new Builder().forBrowser('chrome').build();

  try {
    // Given I am on https://google.com
    await driver.get('https://google.com');

    // When I type in the search box and click submit
    // Google's search input has commonly carried name="q".
    const searchBox = await driver.findElement(By.name('q'));
    await searchBox.sendKeys('testing examples', Key.RETURN);

    // Then I see search results
    const results = await driver.findElements(By.css('#search'));
    if (results.length === 0) {
      throw new Error('Expected to see search results, but none were found.');
    }
  } finally {
    await driver.quit();
  }
}

demo().catch((err) => console.error(err));
Selenium + Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()

try:
    # Given I am on https://google.com
    driver.get("https://google.com")

    # When I type in the search box and click submit
    # Google's search input has commonly carried name="q".
    search_box = driver.find_element(By.NAME, "q")
    search_box.send_keys("testing examples", Keys.RETURN)

    # Then I see search results
    results = driver.find_elements(By.CSS_SELECTOR, "#search")
    assert len(results) > 0, "Expected to see search results, but none were found."
finally:
    driver.quit()
Selenium + Rust
use std::time::Duration;

use thirtyfour::prelude::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // chromedriver must already be listening, e.g. `chromedriver --port=9515`.
    let driver = WebDriver::new("http://localhost:9515", DesiredCapabilities::chrome()).await?;

    // Run the scenario, but always quit the browser afterward.
    let result = scenario(&driver).await;
    driver.quit().await?;
    result
}

async fn scenario(driver: &WebDriver) -> anyhow::Result<()> {
    // Given I am on https://google.com
    driver.goto("https://google.com").await?;

    // When I type in the search box and click submit
    // Google's search input has commonly carried name="q". thirtyfour has
    // no By::Name, so use a CSS attribute selector. "\u{E007}" is the
    // WebDriver Enter key.
    let search_box = driver.query(By::Css("[name='q']")).single().await?;
    search_box.send_keys("testing examples\u{E007}").await?;

    // Then I see search results
    let found = driver
        .query(By::Css("#search"))
        .wait(Duration::from_secs(10), Duration::from_millis(250))
        .exists()
        .await?;
    assert!(found, "Expected to see search results, but none were found.");
    Ok(())
}
Selenium + C#
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

// Selenium Manager finds or downloads a matching chromedriver.
// `using` quits the browser even if the check below throws.
using IWebDriver driver = new ChromeDriver();

// Given I am on https://google.com
driver.Navigate().GoToUrl("https://google.com");

// When I type in the search box and click submit
// Google's search input has commonly carried name="q".
var searchBox = driver.FindElement(By.Name("q"));
searchBox.SendKeys("testing examples" + Keys.Return);

// Then I see search results
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10))
{
    Message = "Expected to see search results, but none were found."
};
wait.Until(d => d.FindElements(By.CssSelector("#search")).Count > 0);
Selenium + Java
import java.time.Duration;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Demo {
    public static void main(String[] args) {
        // Selenium Manager finds or downloads a matching chromedriver.
        WebDriver driver = new ChromeDriver();

        try {
            // Given I am on https://google.com
            driver.get("https://google.com");

            // When I type in the search box and click submit
            // Google's search input has commonly carried name="q".
            WebElement searchBox = driver.findElement(By.name("q"));
            searchBox.sendKeys("testing examples" + Keys.RETURN);

            // Then I see search results
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            wait.until(d -> !d.findElements(By.cssSelector("#search")).isEmpty());
        } finally {
            driver.quit();
        }
    }
}
Selenium + Elixir
require Wallaby.Browser
alias Wallaby.{Browser, Query}

{:ok, _} = Application.ensure_all_started(:wallaby)
{:ok, session} = Wallaby.start_session()

try do
  # Given I am on https://google.com
  session = Browser.visit(session, "https://google.com")

  # When I type in the search box and click submit
  # Google's search input has commonly carried name="q".
  session |> Browser.send_keys(Query.css("[name='q']"), ["testing examples", :enter])

  # Then I see search results
  Browser.assert_has(session, Query.css("#search"))
after
  Wallaby.end_session(session)
end
Selenium + Go
package main

import (
	"time"

	"github.com/tebeka/selenium"
)

func main() {
	// chromedriver must already be running on port 9515.
	driver, err := selenium.NewRemote(selenium.Capabilities{"browserName": "chrome"}, "http://localhost:9515")
	if err != nil {
		panic(err)
	}
	defer driver.Quit()

	// Given I am on https://google.com
	if err := driver.Get("https://google.com"); err != nil {
		panic(err)
	}

	// When I type in the search box and click submit
	// Google's search input has commonly carried name="q".
	searchBox, err := driver.FindElement(selenium.ByName, "q")
	if err != nil {
		panic(err)
	}
	if err := searchBox.SendKeys("testing examples" + selenium.EnterKey); err != nil {
		panic(err)
	}

	// Then I see search results
	err = driver.WaitWithTimeout(func(d selenium.WebDriver) (bool, error) {
		results, err := d.FindElements(selenium.ByCSSSelector, "#search")
		return err == nil && len(results) > 0, nil
	}, 10*time.Second)
	if err != nil {
		panic("Expected to see search results, but none were found.")
	}
}
Selenium + Kotlin
import java.time.Duration
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.support.ui.WebDriverWait

fun main() {
    // Selenium Manager finds or downloads a matching chromedriver.
    val driver = ChromeDriver()

    try {
        // Given I am on https://google.com
        driver.get("https://google.com")

        // When I type in the search box and click submit
        // Google's search input has commonly carried name="q".
        driver.findElement(By.name("q")).sendKeys("testing examples" + Keys.RETURN)

        // Then I see search results
        WebDriverWait(driver, Duration.ofSeconds(10))
            .until { driver.findElements(By.cssSelector("#search")).isNotEmpty() }
    } finally {
        driver.quit()
    }
}
Playwright + JavaScript
import { chromium } from 'playwright';

const browser = await chromium.launch();

try {
  const page = await browser.newPage();

  // Given I am on https://google.com
  await page.goto('https://google.com');

  // When I type in the search box and click submit
  // Google's search input has commonly carried name="q".
  await page.fill('[name="q"]', 'testing examples');
  await page.keyboard.press('Enter');

  // Then I see search results
  await page.waitForSelector('#search');
} finally {
  await browser.close();
}
Playwright + Python
from playwright.sync_api import sync_playwright


def demo() -> None:
    """Search Google and check that results appear."""

    with sync_playwright() as p:
        browser = p.chromium.launch()

        try:
            page = browser.new_page()

            # Given I am on https://google.com
            page.goto("https://google.com")

            # When I type in the search box and click submit
            # Google's search input has commonly carried name="q".
            page.fill('[name="q"]', "testing examples")
            page.keyboard.press("Enter")

            # Then I see search results
            page.wait_for_selector("#search")
        finally:
            browser.close()


if __name__ == "__main__":
    demo()
Playwright + Rust
use playwright_rs::{Page, Playwright, WaitUntil};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let pw = Playwright::launch().await?;
    let browser = pw.chromium().launch().await?;
    let page = browser.new_page().await?;

    // Run the scenario, but always close the browser afterward.
    let result = scenario(&page).await;
    browser.close().await?;
    result
}

async fn scenario(page: &Page) -> anyhow::Result<()> {
    // Given I am on https://google.com
    page.goto("https://google.com", None).await?;

    // When I type in the search box and click submit
    // Google's search input has commonly carried name="q".
    let search_box = page.locator("[name=\"q\"]");
    search_box.fill("testing examples", None).await?;
    search_box.press("Enter", None).await?;
    page.wait_for_load_state(Some(WaitUntil::Load)).await?;

    // Then I see search results
    let count = page.locator("#search").count().await?;
    assert!(count > 0, "Expected to see search results, but none were found.");
    Ok(())
}
Playwright + C#
using Microsoft.Playwright;

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();

// Given I am on https://google.com
await page.GotoAsync("https://google.com");

// When I type in the search box and click submit
// Google's search input has commonly carried name="q".
await page.FillAsync("[name=\"q\"]", "testing examples");
await page.Keyboard.PressAsync("Enter");

// Then I see search results
await page.WaitForSelectorAsync("#search");
Playwright + Java
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class Demo {
    public static void main(String[] args) {
        // try-with-resources shuts down the driver even if a step throws.
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();

            // Given I am on https://google.com
            page.navigate("https://google.com");

            // When I type in the search box and click submit
            // Google's search input has commonly carried name="q".
            page.fill("[name=\"q\"]", "testing examples");
            page.keyboard().press("Enter");

            // Then I see search results
            page.waitForSelector("#search");

            browser.close();
        }
    }
}
Playwright + Elixir
alias Playwright.{Browser, Locator, Page}

{:ok, browser} = Playwright.launch(:chromium)
page = Browser.new_page(browser)

# Given I am on https://google.com
Page.goto(page, "https://google.com")

# When I type in the search box and click submit
# Google's search input has commonly carried name="q".
page |> Page.locator("[name='q']") |> Locator.fill("testing examples")
page |> Page.locator("[name='q']") |> Locator.press("Enter")

# Then I see search results
page |> Page.locator("#search") |> Locator.wait_for()

Browser.close(browser)
Playwright + Go
package main

import "github.com/playwright-community/playwright-go"

func main() {
	pw, err := playwright.Run()
	if err != nil {
		panic(err)
	}
	defer pw.Stop()

	browser, err := pw.Chromium.Launch()
	if err != nil {
		panic(err)
	}
	defer browser.Close()

	page, err := browser.NewPage()
	if err != nil {
		panic(err)
	}

	// Given I am on https://google.com
	if _, err := page.Goto("https://google.com"); err != nil {
		panic(err)
	}

	// When I type in the search box and click submit
	// Google's search input has commonly carried name="q".
	if err := page.Locator("[name=\"q\"]").Fill("testing examples"); err != nil {
		panic(err)
	}
	if err := page.Keyboard().Press("Enter"); err != nil {
		panic(err)
	}

	// Then I see search results
	if err := page.Locator("#search").WaitFor(); err != nil {
		panic(err)
	}
}
Playwright + Kotlin
import com.microsoft.playwright.Playwright

fun main() {
    // `use` shuts down the driver even if a step throws.
    Playwright.create().use { playwright ->
        val browser = playwright.chromium().launch()
        val page = browser.newPage()

        // Given I am on https://google.com
        page.navigate("https://google.com")

        // When I type in the search box and click submit
        // Google's search input has commonly carried name="q".
        page.fill("[name=\"q\"]", "testing examples")
        page.keyboard().press("Enter")

        // Then I see search results
        page.waitForSelector("#search")

        browser.close()
    }
}