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.
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:
For more info about nuget.configclick 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
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
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.
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.
# 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.
# 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
python first_script.py
```Make sure to replace `first_script.py` with the path and name of the example you want to run.
# 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 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
```
# 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
```
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.
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…
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.
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.
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.
Maven
Gradle
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:
Add to project’s gemfile
In your project’s package.json, add requirement to dependencies:
# frozen_string_literal: truerequire'selenium-webdriver'require'selenium/webdriver/support/guards'RSpec.configuredo|config|# Enable flags like --only-failures and --next-failureconfig.example_status_persistence_file_path='.rspec_status'# Disable RSpec exposing methods globally on `Module` and `main`config.disable_monkey_patching!config.expect_with:rspecdo|c|c.syntax=:expectendconfig.beforedo|example|bug_tracker='https://gigithub.com/SeleniumHQ/seleniumhq.github.io/issues'guards=Selenium::WebDriver::Support::Guards.new(example,bug_tracker:bug_tracker)guards.add_condition(:platform,Selenium::WebDriver::Platform.os)guards.add_condition(:ci,Selenium::WebDriver::Platform.ci)results=guards.dispositionsend(*results)ifresultsendconfig.after{@driver&.quit}defstart_session@driver=Selenium::WebDriver.for:chromeenddefstart_firefoxoptions=Selenium::WebDriver::Options.firefox(timeouts:{implicit:1500})@driver=Selenium::WebDriver.for:firefox,options:optionsendend
# 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
python first_script.py
```Make sure to replace `first_script.py` with the path and name of the example you want to run.
# 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 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
```