CosmoCode (Formerly TeachMeSelenium)

First selenium script | Writing and running code

In the Previous Tutorial, we covered the following –

In this tutorial, we’ll write a magical script that does the following operations –

We’ll also execute the script and see it running on Google Chrome browser.

Step 1

Set the path to Chrome Driver so that we can run our test on Google Chrome browser. We are assuming you have saved it in “teachmeselenium” directory under your home directory. Please go through This Tutorial if you missed how to download this driver.

String chromeDriverPath = "~/teachmeselenium/chromedriver";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);

You may have to update chromeDriverPath if it is at some other location like –

String chromeDriverPath = "C:\\teachmeselenium\\chromedriver.exe";

Step 2

Write code to launch Chrome browser.

WebDriver driver = new ChromeDriver();

Eclipse is showing a red-line over WebDriver and ChromeDriver. Is it an error?

We need to resolve these errors by importing correct packages. Move mouse over ‘WebDriver’  and eclipse will prompt to import WebDriver package structure. Click it to import “org.openqa.selenium”. Similarly, move the mouse over ChromeDriver and import package “org.openqa.selenium.chrome

What is a package, man?

Well… It is Java stuff. For now, you can think of Java package as a college library where there are sections for different book genres like Fiction, Technology etc. Each section consists of lots of books of that category. Similarly, a Java package is a placeholder for classes, interfaces, and methods.

OK. Can you please tell me What that line of code is doing – WebDriver driver = new ChromeDriver();

We are declaring object reference of WebDriver Interface and making it point to object of ChromeDriver Class.

Would you please sound less technical?

Well, we are creating an object of ChromeDriver and asking WebDriver to hold it tightly. ‘WebDriver’ has appointed ‘driver’ to do this job. Once that line gets executed it will launch Chrome browser.

I want to know more about Java Classes, Objects and Object References. In fact, I want to know more about Java.

I would recommend going through any tutorial on Java basics. There are very good video course available at Udemy that teaches Java from the very basics.

Step 3

Navigate to https://cosmocode.io/automation-practice-first-selenium-script

driver.get("https://cosmocode.io/automation-practice-first-selenium-script");

It will make the browser navigate to the URL that is passed to the get() method.

Step 4

Print the current page’s title –

String pageTitle = driver.getPageTitle();
System.out.println(pageTitle);

Step 5

Find the search box element.

WebElement searchbox = driver.findElement(By.name("searchBox"));

Step 6

At this stage, the eclipse will prompt us to import packages for WebDriver and By. Do it at the start of the code, before class declaration –

import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

Step 7

Type “Teach me Selenium” in the search box element that we found in the previous step –

searchbox.sendKeys("Teach me Selenium");

Step 8

Find the Search button and click on it –

driver.findElement(By.id("searchBtn")).click();

Step 9

Close the browser and Quit driver.

driver.quit();

Put the complete code in one place, man. I don’t have time to do copy-paste here and there.

Here you go –

import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

String chromeDriverPath = "~/teachmeselenium/chromedriver"; //IMPORTANT: Update the path as per your system
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
WebDriver driver = new ChromeDriver();
driver.get("http://practice.teachmeselenium.com");
driver.get("https://cosmocode.io/automation-practice-first-selenium-script");
String pageTitle = driver.getPageTitle();
System.out.println(pageTitle);
WebElement searchbox = driver.findElement(By.name("searchBox"));
searchbox.sendKeys("Teach me Selenium");
driver.findElement(By.id("searchBtn")).click();
driver.quit();

Step 10

Save the project by clicking on File -> Save All and Run this script.

Wait… I am not able to figure out how to run this script? I am completely new to Java.

There are several ways to do that. I would suggest you to do right-click on the Class file ‘MyFirstSeleniumScript.java’ in the Project Explorer and select Run As -> Java Application

You could have also run it right from the menu bar but sometimes it may confuse the beginners if multiple run configurations are saved. I would suggest you to always run the class file directly as the above screenshot.

Step 1

from selenium import webdriver

Step 2

Set the path of the chrome driver. Please change the path accordingly as per your system.

chrome_driver_path = "C:\selenium\chromedriver.exe"

Hey, wait… I am on Mac. How do I set the path?

Well for Mac you need to set the path of chromedriver. I am assuming you have set it at the following location –

chrome_driver_path = "/usr/local/bin/chromedriver"

Step 3

Next, create an object of Chrome class and pass it chrome driver path

driver = webdriver.Chrome(chrome_driver_path)

The above line of code would open a new instance of Chrome browser.

Step 4

Navigate to the target website

driver.get("https://cosmocode.io/automation-practice-first-selenium-script")

The above line of code will make the browser navigate to practice.teachmeselenium.com

Step 5

Print the current page’s title –

title = driver.title
print(title)

Step 6

Find the search box element.

searchbox = driver.find_element_by_name("searchBox"))

Step 7

Type “Teach me Selenium” in the search box element that we found in the previous step:

searchbox.send_keys("Teach me Selenium")

Step 8

Find the Search button and click on it:

driver.findElement(By.id("searchBtn")).click()

Step 9

Find the Search button and click on it –

driver.find_element_by_id("searchBtn")).click()

Step 10

Close the browser and Quit driver.

driver.quit()

By now we should have following code in our editor –

from selenium import webdriver

#IMPORTANT: Change your path accordingly
chrome_driver_path = "C:\selenium\chromedriver.exe"

driver.get("https://cosmocode.io/automation-practice-first-selenium-script")

title = driver.title
print(title)

searchbox = driver.find_element_by_name("searchBox"))
searchbox.send_keys("Teach me Selenium")

driver.find_element_by_id("searchBtn")).click()

driver.quit()

Step 11

Save this code and run it.

I am not able to figure out how to run this script. I am completely new to Python/Eclipse.

Right-click over the filename in Package Explorer, select Run As -> Python Run

If your environment is set up correctly –

Yes, it happened. I am wondering how would I identify the element on which I need to perform actions like click, type etc?

You need to read the Next Tutorial for this.

Exit mobile version