Getting started If you are new to Selenium, we have a few resources that can help you get up to speed right away.
Selenium supports automation of all the major browsers in the market
through the use of WebDriver .
WebDriver is an API and protocol that defines a language-neutral interface
for controlling the behaviour of web browsers.
Each browser is backed by a specific WebDriver implementation, called a driver .
The driver is the component responsible for delegating down to the browser,
and handles communication to and from Selenium and the browser.
This separation is part of a conscious effort to have browser vendors
take responsibility for the implementation for their browsers.
Selenium makes use of these third party drivers where possible,
but also provides its own drivers maintained by the project
for the cases when this is not a reality.
The Selenium framework ties all of these pieces together
through a user-facing interface that enables the different browser backends
to be used transparently,
enabling cross-browser and cross-platform automation.
Selenium setup is quite different from the setup of other commercial tools.
Before you can start writing Selenium code, you have to
install the language bindings libraries for your language of choice, the browser you
want to use, and the driver for that browser.
Follow the links below to get up and going with Selenium WebDriver.
If you wish to start with a low-code/record and playback tool, please check
Selenium IDE
Once you get things working, if you want to scale up your tests, check out the
Selenium Grid .
1 - Install a Selenium library Setting up the Selenium library for your favourite programming language.
First you need to install the Selenium bindings for your automation project.
The installation process for libraries depends on the language you choose to use.
Make sure you check the Selenium downloads page to make sure
you are using the latest version.
Requirements by language
Java
Python
CSharp
Ruby
JavaScript
Kotlin View the minimum supported Java version here .
Installation of Selenium libraries for Java is accomplished using a build tool.
Maven Specify the dependencies in the project’s pom.xml
file:
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"><?xml version="1.0" encoding="UTF-8"?></span>
<project xmlns= "http://maven.apache.org/POM/4.0.0"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion> 4.0.0</modelVersion>
<groupId> dev.selenium</groupId>
<artifactId> selenium-examples</artifactId>
<version> 1.0.0</version>
<properties>
<surefire.parallel> 1</surefire.parallel>
<maven.compiler.source> 17</maven.compiler.source>
<maven.compiler.target> 17</maven.compiler.target>
<project.build.sourceEncoding> UTF-8</project.build.sourceEncoding>
<selenium.version> 4.31.0</selenium.version>
</properties>
<repositories>
<repository>
<id> sonatype-nexus-snapshots</id>
<url> https://oss.sonatype.org/content/repositories/snapshots/ </url>
<snapshots>
<enabled> true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> org.seleniumhq.selenium</groupId>
<artifactId> selenium-java</artifactId>
<version> ${selenium.version}</version>
</dependency>
<dependency>
<groupId> org.seleniumhq.selenium</groupId>
<artifactId> selenium-grid</artifactId>
<version> ${selenium.version}</version>
</dependency>
<dependency>
<groupId> org.junit.jupiter</groupId>
<artifactId> junit-jupiter-engine</artifactId>
<version> 5.12.1</version>
<scope> test</scope>
</dependency>
<dependency>
<groupId> com.titusfortner</groupId>
<artifactId> selenium-logger</artifactId>
<version> 2.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins</groupId>
<artifactId> maven-surefire-plugin</artifactId>
<version> 3.5.3</version>
<configuration>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
</configurationParameters>
</properties>
<rerunFailingTestsCount> 3</rerunFailingTestsCount>
</configuration>
</plugin>
</plugins>
</build>
</project>
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/java/pom.xml#L30-L34" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Gradle Specify the dependency in the project build.gradle
file as testImplementation
:
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-gradle" data-lang="gradle"><span style="display:flex;"><span><span style="color:#000">plugins</span> <span style="color:#ce5c00;font-weight:bold">{</span>
id 'java'
}
group 'dev.selenium'
version '1.0-SNAPSHOT'
repositories {
mavenCentral ()
}
dependencies {
testImplementation 'org.seleniumhq.selenium:selenium-java:4.31.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.12.1'
}
test {
useJUnitPlatform ()
}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/java/build.gradle#L13-L14" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
The minimum supported Python version for each Selenium version can be found
in “Supported Python Versions” on PyPi .
There are a couple different ways to install Selenium.
Pip Download Alternatively you can download the PyPI Built Distribution
(selenium-x.x.x.-py3-none-any.whl) and install it using pip :
pip install selenium-x.x.x.-py3-none-any.whl
Require in project To use it in a project, add it to the requirements.txt
file:
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-txt" data-lang="txt"><span style="display:flex;"><span>selenium==4.31.0
pytest==8.3.5
trio==0.29.0
pytest-trio==0.8.0
pytest-rerunfailures==15.0
flake8==7.2.0
requests==2.32.3
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/requirements.txt#L1" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
A list of all supported frameworks for each version of Selenium
is available on Nuget
There are a few options for installing Selenium.
Packet Manager Install-Package Selenium.WebDriver
.NET CLI dotnet add package Selenium.WebDriver
CSProj in the project’s csproj
file, specify the dependency as a PackageReference
in ItemGroup
:
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-csproj" data-lang="csproj"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold"><Project</span> <span style="color:#c4a000">Sdk=</span><span style="color:#4e9a06">"Microsoft.NET.Sdk"</span><span style="color:#204a87;font-weight:bold">></span>
<PropertyGroup>
<TargetFramework> net8.0</TargetFramework>
<GenerateProgramFile> false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include= "Microsoft.NET.Test.Sdk" Version= "17.11.1" />
<PackageReference Include= "Microsoft.IdentityModel.Tokens" Version= "7.7.1" />
<PackageReference Include= "MSTest.TestAdapter" Version= "3.6.0" />
<PackageReference Include= "MSTest.TestFramework" Version= "3.6.0" />
<PackageReference Include= "Selenium.Support" Version= "4.31.0" />
<PackageReference Include= "Selenium.WebDriver" Version= "4.31.0" />
</ItemGroup>
<ItemGroup>
<Folder Include= "LocalPackages" />
</ItemGroup>
</Project>
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/dotnet/SeleniumDocs/SeleniumDocs.csproj#L14" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Additional considerations Further items of note for using Visual Studio Code (vscode) and C#
Install the compatible .NET SDK as per the section above.
Also install the vscode extensions (Ctrl-Shift-X) for C# and NuGet.
Follow the instruction here
to create and run the “Hello World” console project using C#.
You may also create a NUnit starter project using the command line dotnet new NUnit
.
Make sure the file %appdata%\NuGet\nuget.config
is configured properly as some developers reported that it will be empty due to some issues.
If nuget.config
is empty, or not configured properly, then .NET builds will fail for Selenium Projects.
Add the following section to the file nuget.config
if it is empty:
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
</packageSources>
...
For more info about nuget.config
click here .
You may have to customize nuget.config
to meet you needs.
Now, go back to vscode, press Ctrl-Shift-P, and type “NuGet Add Package”, and enter the required Selenium packages such as Selenium.WebDriver
.
Press Enter and select the version.
Now you can use the examples in the documentation related to C# with vscode.
You can see the minimum required version of Ruby for any given Selenium version
on rubygems.org
Selenium can be installed two different ways.
Install manually gem install selenium-webdriver
Add to project’s gemfile <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-Gemfile" data-lang="Gemfile"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"># frozen_string_literal: true</span>
source 'https://rubygems.org'
gem 'ffi' , '~> 1.15' , '>= 1.15.5' if Gem . win_platform? # Windows only
gem 'rake' , '~> 13.0'
gem 'rspec' , '~> 3.0'
gem 'rubocop' , '~> 1.35'
gem 'rubocop-rspec' , '~> 3.0'
gem 'selenium-devtools' , '= 0.135.0'
gem 'selenium-webdriver' , '= 4.31.0'
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/ruby/Gemfile#L10" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
You can find the minimum required version of Node for any given version of Selenium in the
Node Support Policy
section on npmjs
Selenium is typically installed using npm.
Install locally npm install selenium-webdriver
Add to project In your project’s package.json
, add requirement to dependencies
:
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span>
"name" : "javascript-examples" ,
"version" : "1.0.0" ,
"scripts" : {
"test" : "npx mocha test/**/*.spec.js –timeout 90000"
},
"author" : "The Selenium project" ,
"license" : "Apache-2.0" ,
"dependencies" : {
"assert" : "2.1.0" ,
"selenium-webdriver" : "4.31.0"
},
"devDependencies" : {
"mocha" : "11.1.0"
}
}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/javascript/package.json#L14" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Use the Java bindings for Kotlin.
Next Step Create your first Selenium script
2 - Write your first Selenium script Step-by-step instructions for constructing a Selenium script
Once you have Selenium 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
Click on the link to “View full example on GitHub” to see the code in context.
1. Start the session For more details on starting a session read our documentation on driver sessions
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
2. Take action on browser In this example we are navigating to a web page.
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
There are a bunch of types of information about the browser you
can request, including window handles, browser size / position, cookies, alerts, etc.
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
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 .
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
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
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
6. Take action on element There are only a handful of actions to take on an element ,
but you will use them frequently.
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
Elements store a lot of information that can be requested .
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
8. 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.
See Quitting Sessions .
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
import java.time.Duration ;
public class FirstScript {
public static void main ( String [] args ) {
WebDriver driver = new ChromeDriver ();
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
driver . getTitle ();
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
message . getText ();
driver . quit ();
}
}
Show full example 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
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" )
text = message . text
driver . quit ()
Show full example using System ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted ;
public static class FirstScript
{
public static void Main ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
driver . Quit ();
}
}
Show full example require 'selenium-webdriver'
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
driver . title
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
message . text
driver . quit
Show full example 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 ();
}
}())
Show full example package dev.selenium.getting_started
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
@TestInstance ( TestInstance . Lifecycle . PER_CLASS )
class FirstScriptTest {
private lateinit var driver : WebDriver
@Test
fun eightComponents () {
driver = ChromeDriver ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
val title = driver . title
assertEquals ( "Web form" , title )
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ))
var textBox = driver . findElement ( By . name ( "my-text" ))
val submitButton = driver . findElement ( By . cssSelector ( "button" ))
textBox . sendKeys ( "Selenium" )
submitButton . click ()
val message = driver . findElement ( By . id ( "message" ))
val value = message . getText ()
assertEquals ( "Received!" , value )
driver . quit ()
}
}
Running Selenium File
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example # Running Selenium Java Tests
The following steps will guide you on how to
run Selenium Java tests using a repository
of `SeleniumHQ/seleniumhq.github.io` examples.
## Initial Setup
### Prerequisites
Ensure that Java Development Kit (JDK) and Maven
are installed on your system. If they are not installed,
you will need to download and install them. You can
find detailed installation guides for both on their
respective official sites.
### Clone the repository
First, we need to get the Selenium Java examples
on your local machine. This can be done by
cloning the `SeleniumHQ/seleniumhq.github.io` Git repository.
Run the following command in your terminal:
```bash
git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git
```
## Navigate to the java directory
After cloning the repository, navigate into the
directory where the Selenium Java examples are
located. Run the following command:
```bash
cd seleniumhq.github.io/examples/java
```
## Running the Tests
### Install dependencies
Before running the tests, we need to install all
necessary dependencies. Maven, a software
project management tool, can do this for us.
Run the following command:
```bash
mvn test-compile
```
### Run all tests
To verify if everything is installed correctly and
functioning properly, we should run all
available tests. This can be done with the following command:
```bash
mvn test
```
Please be patient! If this is your first time running these tests,
it might take a while to download and verify all necessary browser drivers.
## Execute a specific example
To run a specific Selenium Java example, use the following command:
```bash
mvn exec:java -D"exec.mainClass" = "dev.selenium.getting_started.FirstScript" -D"exec.classpathScope" = test
```
Make sure to replace `dev.selenium.getting_started.FirstScript` with the path and name of the example you want to run.
Show full example # Running all tests from Selenium python example
Follow these steps to run all test example from selenium python
1. Clone this repository
```
git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git
```
2. Navigate to `python` directory
```
cd seleniumhq.github.io/examples/python
```
3. Install dependencies using pip
```
pip install -r requirements.txt
```
> if you are on a different python version, for example python3.x you may have to replace `pip` with `pip3`
4. Run all tests
```
pytest
```
> Please keep some patience - If you are doing it for the first time, it will take a little while to verify and download the browser drivers
## Execute a specific example
To run a specific Selenium Python example, use the following command:
```bash
pytest path/to/test_script.py
```
Make sure to replace `path/to/test_script.py` with the path and name of the example you want to run.
Show full example # Running all tests from Selenium ruby example
Follow these steps to run all test example from selenium ruby
1. Clone this repository
```
git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git
```
2. Navigate to `ruby` directory
```
cd seleniumhq.github.io/examples/ruby
```
3. Install dependencies using bundler
```
bundler install
```
4. Run all tests
```
bundle exec rspec
```
> Please keep some patience - If you are doing it for the first time, it will take a little while to verify and download the browser drivers
# Execute a ruby script
Use this command to run a ruby script and follow the first script example
```
ruby example_script.rb
```
Show full example # Running all tests from Selenium javascript example
Follow these steps to run all test example from selenium javascript
1. Clone this repository
```
git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git
```
2. Navigate to `javascript` directory
```
cd seleniumhq.github.io/examples/javascript
```
3. Install dependencies using node
```
npm install
```
4. Run all all tests
```
npm test
```
> Please keep some patience - If you are doing it for the first time, it will take a little while to verify and download the browser drivers
# Execute a javascript test
Use this command to run a JavaScript and follow the first script example
```
node example_script.spec.js
```
Next Steps Most Selenium users execute many sessions and need to organize them to minimize duplication and keep the code
more maintainable. Read on to learn about how to put this code into context for your use case with
Using Selenium .
3 - Organizing and Executing Selenium Code Scaling Selenium execution with an IDE and a Test Runner library
Content Help Note: This section needs additional and/or updated content
This page is very incomplete and has placeholders for things that need to be added or expounded on.
Check our
contribution guidelines if you’d like to help.
If you want to run more than a handful of one-off scripts, you need to
be able to organize and work with your code. This page should give you
ideas for how to actually do productive things with your Selenium code.
Common Uses Most people use Selenium to execute automated tests for web applications,
but Selenium supports any use case of browser automation.
Repetitive Tasks Perhaps you need to log into a website and download something, or submit a form.
You can create a Selenium script to run with a service at preset times.
Web Scraping Are you looking to collect data from a site that doesn’t have an API? Selenium
will let you do this, but please make sure you are familiar with the website’s
terms of service as some websites do not permit it and others will even block Selenium.
Testing Running Selenium for testing requires making assertions on actions taken by Selenium.
So a good assertion library is required. Additional features to provide structure for tests
require use of Test Runner .
IDEs Regardless of how you use Selenium code,
you won’t be very effective writing or executing it without a good
Integrated Developer Environment. Here are some common options…
Test Runner Even if you aren’t using Selenium for testing, if you have advanced use cases, it might make
sense to use a test runner to better organize your code. Being able to use before/after hooks
and run things in groups or in parallel can be very useful.
Choosing There are many different test runners available.
All the code examples in this documentation can be found in (or is being moved to) our
example directories that use test runners and get executed every release to ensure all the code is correct and updated.
Here is a list of test runners with links. The first item is the one that is used by this repository and the one
that will be used for all examples on this page.
Java
Python
CSharp
Ruby
JavaScript
Kotlin JUnit - A widely-used testing framework for Java-based Selenium tests.TestNG - Offers extra features like parallel test execution and parameterized tests.pytest - A preferred choice for many, thanks to its simplicity and powerful plugins.unittest - Python’s standard library testing framework.NUnit - A popular unit-testing framework for .NET.MS Test - Microsoft’s own unit testing framework.RSpec - The most widely used testing library for running Selenium tests in Ruby.Minitest - A lightweight testing framework that comes with Ruby standard library.Jest - Primarily known as a testing framework for React, it can also be used for Selenium tests.Mocha - The most common JS library for running Selenium tests.Kotest - A flexible and comprehensive testing framework specifically designed for Kotlin.JUnit5 - The standard Java testing framework, fully compatible with Kotlin.Installing This is very similar to what was required in Install a Selenium Library .
This code is only showing examples for what is being used in our Documentation Examples project.
Java
Python
CSharp
Ruby
JavaScript
Kotlin To use it in a project, add it to the requirements.txt
file:
in the project’s csproj
file, specify the dependency as a PackageReference
in ItemGroup
:
In your project’s package.json
, add requirement to dependencies
:
Asserting
Java
Python
CSharp
Ruby
JavaScript
Kotlin Show full example package dev.selenium.getting_started ;
import static org.junit.jupiter.api.Assertions.assertEquals ;
import java.time.Duration ;
import org.junit.jupiter.api.AfterEach ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
public class UsingSeleniumTest {
WebDriver driver ;
@BeforeEach
public void setup () {
driver = new ChromeDriver ();
}
@Test
public void eightComponents () {
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
String title = driver . getTitle ();
assertEquals ( "Web form" , title );
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
String value = message . getText ();
assertEquals ( "Received!" , value );
}
@AfterEach
public void teardown () {
driver . quit ();
}
}
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">from</span> <span style="color:#000">selenium</span> <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">webdriver</span>
from selenium.webdriver.common.by import By
def test_eight_components ():
driver = setup ()
title = driver . title
assert 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" )
value = message . text
assert value == "Received!"
teardown ( driver )
def setup ():
driver = webdriver . Chrome ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
return driver
def teardown ( driver ):
driver . quit ()
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/tests/getting_started/using_selenium_tests.py#L8-L9" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Show full example using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted
{
[TestClass]
public class UsingSeleniumTest
{
[TestMethod]
public void EightComponents ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
Assert . AreEqual ( "Web form" , title );
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
Assert . AreEqual ( "Received!" , value );
driver . Quit ();
}
}
}
Show full example # frozen_string_literal: true
require 'selenium-webdriver'
RSpec . describe 'Using Selenium' do
it 'uses eight components' do
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
title = driver . title
expect ( title ) . to eq ( 'Web form' )
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
value = message . text
expect ( value ) . to eq ( 'Received!' )
driver . quit
end
end
Show full example const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( "assert" );
describe ( 'First script' , function () {
let driver ;
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
});
it ( 'First Selenium script with mocha' , async function () {
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 );
});
after ( async () => await driver . quit ());
});
Setting Up and Tearing Down
Java
Python
CSharp
Ruby
JavaScript
Kotlin Set Up <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">dev.selenium.getting_started</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline">
import static org.junit.jupiter.api.Assertions.assertEquals ;
import java.time.Duration ;
import org.junit.jupiter.api.AfterEach ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
public class UsingSeleniumTest {
WebDriver driver ;
@BeforeEach
public void setup () {
driver = new ChromeDriver ();
}
@Test
public void eightComponents () {
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
String title = driver . getTitle ();
assertEquals ( "Web form" , title );
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
String value = message . getText ();
assertEquals ( "Received!" , value );
}
@AfterEach
public void teardown () {
driver . quit ();
}
}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L19-L22" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Tear Down <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">dev.selenium.getting_started</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline">
import static org.junit.jupiter.api.Assertions.assertEquals ;
import java.time.Duration ;
import org.junit.jupiter.api.AfterEach ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
public class UsingSeleniumTest {
WebDriver driver ;
@BeforeEach
public void setup () {
driver = new ChromeDriver ();
}
@Test
public void eightComponents () {
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
String title = driver . getTitle ();
assertEquals ( "Web form" , title );
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
String value = message . getText ();
assertEquals ( "Received!" , value );
}
@AfterEach
public void teardown () {
driver . quit ();
}
}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java#L45-L48" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Set Up <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">from</span> <span style="color:#000">selenium</span> <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">webdriver</span>
from selenium.webdriver.common.by import By
def test_eight_components ():
driver = setup ()
title = driver . title
assert 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" )
value = message . text
assert value == "Received!"
teardown ( driver )
def setup ():
driver = webdriver . Chrome ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
return driver
def teardown ( driver ):
driver . quit ()
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/tests/getting_started/using_selenium_tests.py#L25-L28" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Tear Down <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">from</span> <span style="color:#000">selenium</span> <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">webdriver</span>
from selenium.webdriver.common.by import By
def test_eight_components ():
driver = setup ()
title = driver . title
assert 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" )
value = message . text
assert value == "Received!"
teardown ( driver )
def setup ():
driver = webdriver . Chrome ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
return driver
def teardown ( driver ):
driver . quit ()
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/tests/getting_started/using_selenium_tests.py#L30-31" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Set Up <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-rb" data-lang="rb"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"># frozen_string_literal: true</span>
require 'selenium-webdriver'
RSpec . describe 'Using Selenium' do
it 'uses eight components' do
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
title = driver . title
expect ( title ) . to eq ( 'Web form' )
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
value = message . text
expect ( value ) . to eq ( 'Received!' )
driver . quit
end
end
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/ruby/spec/getting_started/using_selenium_spec.rb#L7" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Tear Down <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-rb" data-lang="rb"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"># frozen_string_literal: true</span>
require 'selenium-webdriver'
RSpec . describe 'Using Selenium' do
it 'uses eight components' do
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
title = driver . title
expect ( title ) . to eq ( 'Web form' )
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
value = message . text
expect ( value ) . to eq ( 'Received!' )
driver . quit
end
end
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/ruby/spec/getting_started/using_selenium_spec.rb#L26" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
### Set Up
Show full example const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( "assert" );
describe ( 'First script' , function () {
let driver ;
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
});
it ( 'First Selenium script with mocha' , async function () {
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 );
});
after ( async () => await driver . quit ());
});
### Tear Down
Show full example const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( "assert" );
describe ( 'First script' , function () {
let driver ;
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
});
it ( 'First Selenium script with mocha' , async function () {
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 );
});
after ( async () => await driver . quit ());
});
Executing
Java
Python
CSharp
Ruby
JavaScript
Kotlin <details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-md" data-lang="md"><span style="display:flex;"><span><span style="color:#000080;font-weight:bold"># Running all tests from Selenium python example
Follow these steps to run all test example from selenium python
1. Clone this repository
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
2. Navigate to python
directory
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>cd seleniumhq.github.io/examples/python </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
3. Install dependencies using pip
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>pip install -r requirements.txt </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
> if you are on a different python version, for example python3.x you may have to replace pip
with pip3
4. Run all tests
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>pytest </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
> Please keep some patience - If you are doing it for the first time, it will take a little while to verify and download the browser drivers
## Execute a specific example
To run a specific Selenium Python example, use the following command:
bash </span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>pytest path/to/test_script.py </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
Make sure to replace path/to/test_script.py
with the path and name of the example you want to run.
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/README.md#L35" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
<details class="mt-3">
<summary>Show full example</summary>
<div class="pt-2">
<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-md" data-lang="md"><span style="display:flex;"><span><span style="color:#000080;font-weight:bold"># Running all tests from Selenium ruby example
Follow these steps to run all test example from selenium ruby
1. Clone this repository
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>git clone https://github.com/SeleniumHQ/seleniumhq.github.io.git </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
2. Navigate to ruby
directory
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>cd seleniumhq.github.io/examples/ruby </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
3. Install dependencies using bundler
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>bundler install </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
4. Run all tests
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>bundle exec rspec </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
> Please keep some patience - If you are doing it for the first time, it will take a little while to verify and download the browser drivers
# Execute a ruby script
Use this command to run a ruby script and follow the first script example
</span></span></span><span style="display:flex;"><span><span style="color:#4e9a06"></span>ruby example_script.rb </span></span><span style="display:flex;"><span><span style="color:#4e9a06">
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/ruby/README.md#L26" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Mocha mocha runningTests.spec.js
npx npx mocha runningTests.spec.js
Examples In First script , we saw each of the components of a Selenium script.
Here’s an example of that code using a test runner:
Java
Python
CSharp
Ruby
JavaScript
Kotlin package dev.selenium.getting_started ;
import static org.junit.jupiter.api.Assertions.assertEquals ;
import java.time.Duration ;
import org.junit.jupiter.api.AfterEach ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.Test ;
import org.openqa.selenium.By ;
import org.openqa.selenium.WebDriver ;
import org.openqa.selenium.WebElement ;
import org.openqa.selenium.chrome.ChromeDriver ;
public class UsingSeleniumTest {
WebDriver driver ;
@BeforeEach
public void setup () {
driver = new ChromeDriver ();
}
@Test
public void eightComponents () {
driver . manage (). timeouts (). implicitlyWait ( Duration . ofMillis ( 500 ));
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" );
String title = driver . getTitle ();
assertEquals ( "Web form" , title );
WebElement textBox = driver . findElement ( By . name ( "my-text" ));
WebElement submitButton = driver . findElement ( By . cssSelector ( "button" ));
textBox . sendKeys ( "Selenium" );
submitButton . click ();
WebElement message = driver . findElement ( By . id ( "message" ));
String value = message . getText ();
assertEquals ( "Received!" , value );
}
@AfterEach
public void teardown () {
driver . quit ();
}
}
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_eight_components ():
driver = setup ()
title = driver . title
assert 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" )
value = message . text
assert value == "Received!"
teardown ( driver )
def setup ():
driver = webdriver . Chrome ()
driver . get ( "https://www.selenium.dev/selenium/web/web-form.html" )
return driver
def teardown ( driver ):
driver . quit ()
using System ;
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using OpenQA.Selenium ;
using OpenQA.Selenium.Chrome ;
namespace SeleniumDocs.GettingStarted
{
[TestClass]
public class UsingSeleniumTest
{
[TestMethod]
public void EightComponents ()
{
IWebDriver driver = new ChromeDriver ();
driver . Navigate (). GoToUrl ( "https://www.selenium.dev/selenium/web/web-form.html" );
var title = driver . Title ;
Assert . AreEqual ( "Web form" , title );
driver . Manage (). Timeouts (). ImplicitWait = TimeSpan . FromMilliseconds ( 500 );
var textBox = driver . FindElement ( By . Name ( "my-text" ));
var submitButton = driver . FindElement ( By . TagName ( "button" ));
textBox . SendKeys ( "Selenium" );
submitButton . Click ();
var message = driver . FindElement ( By . Id ( "message" ));
var value = message . Text ;
Assert . AreEqual ( "Received!" , value );
driver . Quit ();
}
}
}
# frozen_string_literal: true
require 'selenium-webdriver'
RSpec . describe 'Using Selenium' do
it 'uses eight components' do
driver = Selenium :: WebDriver . for :chrome
driver . get ( 'https://www.selenium.dev/selenium/web/web-form.html' )
title = driver . title
expect ( title ) . to eq ( 'Web form' )
driver . manage . timeouts . implicit_wait = 500
text_box = driver . find_element ( name : 'my-text' )
submit_button = driver . find_element ( tag_name : 'button' )
text_box . send_keys ( 'Selenium' )
submit_button . click
message = driver . find_element ( id : 'message' )
value = message . text
expect ( value ) . to eq ( 'Received!' )
driver . quit
end
end
const { By , Builder } = require ( 'selenium-webdriver' );
const assert = require ( "assert" );
describe ( 'First script' , function () {
let driver ;
before ( async function () {
driver = await new Builder (). forBrowser ( 'chrome' ). build ();
});
it ( 'First Selenium script with mocha' , async function () {
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 );
});
after ( async () => await 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 .
Development Partners
Selenium Level Sponsors Support the Selenium Project Learn more or view the full list of sponsors.