Writing Your First Script

In the previous chapter, we have done necessary setup to get started with our selenium automation. In this chapter let's create a simple Selenium script to verify that your setup is working correctly. Let's create a script using a programing language of your choice.

Java
C#
Python
Javascript
Copy
//myFirstScript.java

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 FirstTestScript {
    public static void main(String[] args)
    {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.testautomationstudio.com/demo/home/");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        WebElement contentTitleElement = driver.findElement(By.className("contentTitle"));
        if (contentTitleElement.isDisplayed())
        {
            System.out.println("Element found");
        } else
        {
            System.out.println("Element not found");
        }
        driver.quit();
    }
}
Copy
//FirstScript.cs

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;

namespace FirstScript
{
    internal class FirstScript
    {

        public static void Main(string[] args) 
        {
            IWebDriver driver = new ChromeDriver();
            driver.Url = "https://www.testautomationstudio.com/demo/home/";
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
            IWebElement contentTitleElement = driver.FindElement(By.ClassName("contentTitle"));
            if (contentTitleElement.Displayed)
            {
                Console.WriteLine("Element found");
            }
            else
            {
                Console.WriteLine("Element not found");
            }
            driver.Quit();
        }
        
    }
}
Copy
# first_script.py

from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://www.testautomationstudio.com/demo/home/")
element = driver.find_elements_by_class_name("contentTitle")
if element.is_displayed():
  print "Element found"
else:
  print "Element not found"
driver.quit()
Javascript Code Coming Soon.

Following are the line by line explanation of above example.

  1. We have created an instance of ChromeDriver to create new Chrome driver session.
  2. Get method of chromeDriver instance is called to load the page and wait till it loads.
  3. implicitlyWait method is called to set implicit wait for a duration of 30 seconds.
  4. Created an WebElement object using classname to identify the page header.
  5. A conditional check of isDisplayed method method of WebElement instance.
  6. WebDriver method quit is called to end the WebDriver session.

Run the above example in the respective IDE. You should see a chrome browser is opened and it should print Element found in console without error

Note:

If you are getting any error related to webdriver version, you should check the version of the chrome browser and accordingly download the webdriver driver.