Write your first Selenium script

Step-by-step instructions for constructing a Selenium script

Once you have Selenium installed and Drivers installed, you’re ready to write Selenium code.

Eight Basic Components

Everything Selenium does is send the browser commands to do something or send requests for information. Most of what you’ll do with Selenium is a combination of these basic commands:

1. Start the session

For more details on starting a session read our documentation on opening and closing a browser

    WebDriver driver = new ChromeDriver();
    
    driver = webdriver.Chrome()
    
    var driver = new ChromeDriver();
    
    driver = Selenium::WebDriver.for :chrome
    
    let driver = await new Builder().forBrowser('chrome').build();
    
    val driver = ChromeDriver()
    

2. Take action on browser

In this example we are navigating to a web page.

    driver.get("https://selenium.dev");
    
    driver.get("http://www.google.com")
    
    driver.Navigate().GoToUrl("https://www.google.com");
    
    driver.get 'https://google.com'
    
    await driver.get('https://www.google.com');
    
    driver.get("https://google.com")
    

3. Request browser information

There are a bunch of types of information about the browser you can request, including window handles, browser size / position, cookies, alerts, etc.

    driver.getTitle(); // => "Google"
    
    driver.title # => "Google"
    
    driver.Title; // => "Google"
    
    driver.title # => 'Google'
    
    await driver.getTitle(); // => "Google"
    
    driver.getTitle() // => "Google"
    

4. Establish Waiting Strategy

Synchronizing the code with the current state of the browser is one of the biggest challenges with Selenium, and doing it well is an advanced topic.

Essentially you want to make sure that the element is on the page before you attempt to locate it and the element is in an interactable state before you attempt to interact with it.

An implicit wait is rarely the best solution, but it’s the easiest to demonstrate here, so we’ll use it as a placeholder.

Read more about Waiting strategies.

    driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
    
    driver.implicitly_wait(0.5)
    
    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
    
    driver.manage.timeouts.implicit_wait = 500
    
    driver.manage().setTimeouts({implicit: 0.5 })
    
    driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500))
    

5. Find an element

The majority of commands in most Selenium sessions are element related, and you can’t interact with one without first finding an element

    WebElement searchBox = driver.findElement(By.name("q"));
    WebElement searchButton = driver.findElement(By.name("btnK"));
    
    search_box = driver.find_element(By.NAME, "q")
    search_button = driver.find_element(By.NAME, "btnK")
    
    var searchBox = driver.FindElement(By.Name("q"));
    var searchButton = driver.FindElement(By.Name("btnK"))
    
    search_box = driver.find_element(name: 'q')
    search_button = driver.find_element(name: 'btnK')
    
    let searchBox = await driver.findElement(By.name('q'));
    let searchButton = await driver.findElement(By.name('btnK'));
    
    val searchBox = driver.findElement(By.name("q"));
    val searchButton = driver.findElement(By.name("btnK"))
    

6. Take action on element

There are only a handful of actions to take on an element, but you will use them frequently.

    searchBox.sendKeys("Selenium");
    searchButton.click();
    
    search_box.send_keys("Selenium")
    search_button.click()
    
    searchBox.SendKeys("Selenium");
    searchButton.Click();
    
    search_box.send_keys 'Selenium'
    search_button.click
    
    await searchBox.sendKeys('Selenium');
    await searchButton.click();
    
    searchBox.sendKeys("Selenium")
    searchButton.click()
    

7. Request element information

Elements store a lot of information that can be requested. Notice that we need to relocate the search box because the DOM has changed since we first located it.

    driver.findElement(By.name("q")).getAttribute("value"); // => "Selenium"
    
    driver.find_element(By.NAME, "q").get_attribute("value") # => "Selenium"
    
    driver.FindElement(By.Name("q")).GetAttribute("value"); // => "Selenium"
    
    driver.find_element(name: 'q').attribute('value') # => "Selenium"
    
    await driver.findElement(By.name('q')).getAttribute("value"); // => 'Selenium'
    
    driver.findElement(By.name("q")).getAttribute("value"); // => "Selenium"
    

7. End the session

This ends the driver process, which by default closes the browser as well. No more commands can be sent to this driver instance.

    driver.quit();
    
    driver.quit()
    
    driver.Quit();
    
    driver.quit
    
    await driver.quit();
    
    driver.quit()
    

Putting everything together

Let’s combine these 8 things into a complete script.

Follow the link at the bottom of the tab to see an example of the code as it would be executed with a test runner instead of a standalone file.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class HelloSelenium {
    public static void main(String[] args) {
        driver = new ChromeDriver();

        driver.get("https://google.com");
        
        driver.getTitle(); // => "Google"

        driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
        
        WebElement searchBox = driver.findElement(By.name("q"));
        WebElement searchButton = driver.findElement(By.name("btnK"));
        
        searchBox.sendKeys("Selenium");
        searchButton.click();
        
        searchBox = driver.findElement(By.name("q"));
        searchBox.getAttribute("value"); // => "Selenium"
        
        driver.quit();
    }
}
from selenium import webdriver
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()

driver.get("https://www.google.com")

driver.title # => "Google"

driver.implicitly_wait(0.5)

search_box = driver.find_element(By.NAME, "q")
search_button = driver.find_element(By.NAME, "btnK")

search_box.send_keys("Selenium")
search_button.click()

driver.find_element(By.NAME, "q").get_attribute("value") # => "Selenium"

driver.quit()
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class HelloSelenium {
    static void Main() {
        var driver = new ChromeDriver();

        driver.Navigate().GoToUrl("https://www.google.com");

        driver.Title; // => "Google"

        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);

        var searchBox = driver.FindElement(By.Name("q"));
        var searchButton = driver.FindElement(By.Name("btnK"))

        searchBox.SendKeys("Selenium");
        searchButton.Click();

        driver.FindElement(By.Name("q")).GetAttribute("value"); // => "Selenium"

        driver.Quit();
    }
}
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

driver.get 'https://google.com'

driver.title # => 'Google'

driver.manage.timeouts.implicit_wait = 500

search_box = driver.find_element(name: 'q')
search_button = driver.find_element(name: 'btnK')

search_box.send_keys 'Selenium'
search_button.click

driver.find_element(name: 'q').attribute('value') # => "Selenium"

driver.quit
const {Builder, By, Key, until} = require('selenium-webdriver');

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

    await driver.get('https://www.google.com');

    await driver.getTitle(); // => "Google"

    driver.manage().setTimeouts({implicit: 0.5 })

    let searchBox = await driver.findElement(By.name('q'));
    let searchButton = await driver.findElement(By.name('btnK'));

    await searchBox.sendKeys('Selenium');
    await searchButton.click();

    await driver.findElement(By.name('q')).getAttribute("value"); // => 'Selenium'

    await driver.quit();
})();
import org.openqa.selenium.By
import org.openqa.selenium.chrome.ChromeDriver

fun main() {
    val driver = ChromeDriver()

    driver.get("https://google.com")

    driver.getTitle(); // => "Google"

    driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500))

    val searchBox = driver.findElement(By.name("q"));
    val searchButton = driver.findElement(By.name("btnK"));

    searchBox.sendKeys("Selenium");
    searchButton.click();

    driver.findElement(By.name("q")).getAttribute("value"); // => "Selenium"

    driver.quit()
}

Next Steps

Take what you’ve learned and build out your Selenium code.

As you find more functionality that you need, read up on the rest of our WebDriver documentation.