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 /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
2. Take action on browser In this example we are navigating to a web page.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
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 /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
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 /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
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 /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
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 /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
Elements store a lot of information that can be requested .
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
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 /examples/java/src/test/java/dev/selenium/getting_started/FirstScript.java
Copy
Close
/examples/python/tests/getting_started/first_script.py
Copy
Close
/examples/dotnet/SeleniumDocs/GettingStarted/FirstScript.cs
Copy
Close
/examples/ruby/spec/getting_started/first_script.rb
Copy
Close
/examples/javascript/test/getting_started/firstScript.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/getting_started/FirstScriptTest.kt
Copy
Close
Running Selenium File
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/README.md
Copy
Close
/examples/python/README.md
Copy
Close
/examples/dotnet/README.md
Copy
Close
/examples/ruby/README.md
Copy
Close
/examples/javascript/README.md
Copy
Close
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 .