Write your first Selenium script (2024)

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

 WebDriver driver = new ChromeDriver();
driver = webdriver.Chrome()
 IWebDriver driver = new ChromeDriver();
driver = Selenium::WebDriver.for :chrome
 driver = await new Builder().forBrowser(Browser.CHROME).build();
 driver = ChromeDriver()

2. Take action on browser

In this example we are navigating to a web page.

 driver.get("https://www.selenium.dev/selenium/web/web-form.html");
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
 driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html");
driver.get('https://www.selenium.dev/selenium/web/web-form.html')
 await driver.get('https://www.selenium.dev/selenium/web/web-form.html');
 driver.get("https://www.selenium.dev/selenium/web/web-form.html")

3. Request browser information

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

4. Establish Waiting Strategy

Synchronizing the code with the current state of the browser is one of the biggest challengeswith 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 itand 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, sowe’ll use it as a placeholder.

Read more about Waiting strategies.

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

5. Find an element

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

 WebElement textBox = driver.findElement(By.name("my-text")); WebElement submitButton = driver.findElement(By.cssSelector("button"));
text_box = driver.find_element(by=By.NAME, value="my-text")submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
 var textBox = driver.FindElement(By.Name("my-text")); var submitButton = driver.FindElement(By.TagName("button"));
text_box = driver.find_element(name: 'my-text')submit_button = driver.find_element(tag_name: 'button')
 let textBox = await driver.findElement(By.name('my-text')); let submitButton = await driver.findElement(By.css('button'));
 var textBox = driver.findElement(By.name("my-text")) val submitButton = driver.findElement(By.cssSelector("button"))

6. Take action on element

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

 textBox.sendKeys("Selenium"); submitButton.click();
text_box.send_keys("Selenium")submit_button.click()
 textBox.SendKeys("Selenium"); submitButton.Click();
text_box.send_keys('Selenium')submit_button.click
 await textBox.sendKeys('Selenium'); await submitButton.click();
 textBox.sendKeys("Selenium") submitButton.click()

7. Request element information

Elements store a lot of information that can be requested.

 message.getText();
text = message.text
 var value = message.Text;
 let value = await message.getText();
 val value = message.getText()

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.

Running Selenium File

Add Example

Add Example

Add Example

ruby example_script.rb
node example_script.spec.js

Add Example

Next Steps

Most Selenium users execute many sessions and need to organize them to minimize duplication and keep the codemore maintainable. Read on to learn about how to put this code into context for your use case withUsing Selenium.

Write your first Selenium script (2024)
Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6342

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.