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
Kotlin
//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("http://localhost:8080/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();
}
}
//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 = "http//localhost:8080/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();
}
}
}
# first_script.py
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://localhost:8080/demo/home/")
element = driver.find_elements_by_class_name("contentTitle")
if element.is_displayed():
print "Element found"
else:
print "Element not found"
driver.quit()
// myFirstScript.js
const { Builder, By, Key, until } = require('selenium-webdriver');
(async function example() {
let driver = await new Builder().forBrowser('chrome').build();
try
{
await driver.get('http://localhost:8080/demo/home/');
await driver.manage().window().maximize();
await driver.manage().setTimeouts({ implicit: 30000 });
let contentTitleElement = driver.findElement(By.className("contentTitle"));
if (contentTitleElement.isDisplayed())
{
console.log("Element found");
}
else
{
console.log("Element not found");
}
}
catch (error) {
console.error('Test Failed:', error);
}
finally {
await driver.quit();
}
})();
// myFirstScript.kt
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import java.time.Duration
class FirstTestScript
{
fun main()
{
val driver: WebDriver = ChromeDriver()
driver["http://localhost:8080/demo/home/"]
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
val contentTitleElement = driver.findElement(By.className("contentTitle"))
if (contentTitleElement.isDisplayed)
{
println("Element found")
}
else
{
println("Element not found")
}
driver.quit()
}
}
Following are the line by line explanation of above example.
- We have created an instance of ChromeDriver to create new Chrome driver session.
- Get method of chromeDriver instance is called to load the page and wait till it loads.
- implicitlyWait method is called to set implicit wait for a duration of 30 seconds.
- Created an WebElement object using classname to identify the page header.
- A conditional check of isDisplayed method method of WebElement instance.
- 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.