Appearance
Usage
The examples below use Selenium's own public test form at https://www.selenium.dev/selenium/web/web-form.html so they run without any target-site credentials.
Python
python
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
title = driver.title
print(title) # "Web form"
driver.implicitly_wait(0.5)
text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
text_box.send_keys("Selenium")
submit_button.click()
message = driver.find_element(by=By.ID, value="message")
print(message.text) # "Received!"
driver.quit()Run it:
bash
python first_script.pyOr with pytest:
bash
pytest first_script.pyJavaScript / Node.js
javascript
const { By, Builder, Browser } = require('selenium-webdriver');
const assert = require('assert');
(async function firstTest() {
let driver;
try {
driver = await new Builder().forBrowser(Browser.CHROME).build();
await driver.get('https://www.selenium.dev/selenium/web/web-form.html');
let title = await driver.getTitle();
assert.equal('Web form', title);
await driver.manage().setTimeouts({ implicit: 500 });
let textBox = await driver.findElement(By.name('my-text'));
let submitButton = await driver.findElement(By.css('button'));
await textBox.sendKeys('Selenium');
await submitButton.click();
let message = await driver.findElement(By.id('message'));
let value = await message.getText();
assert.equal('Received!', value);
} catch (e) {
console.log(e);
} finally {
await driver.quit();
}
})();Run it:
bash
node first_script.jsHeadless Chrome (Python)
To run without a visible browser window, set the headless option before creating the driver:
python
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)Notes
implicitly_waittells WebDriver to poll the DOM for up to N seconds when locating elements. It is global per session.- Always call
driver.quit()at the end. Callingdriver.close()only closes the current window;quit()kills the entire session and the browser process. - Selenium Manager (bundled) downloads the matching ChromeDriver automatically on first run. No manual driver setup is needed on fresh installs.