Selenium with Python Tutorial 2- Run your first Selenium Test script on Google Chrome using Python

Ayush Srivastava
3 min readMay 19, 2021

--

How to run first test script on Google Chrome Browser in Selenium using Python?

In previous tutorial we have seen that how to install python, selenium and Pycharm IDE. If you haven’t go through it, here is the link –

👉👉 https://ayush-srivastava.medium.com/selenium-with-python-tutorial-in-2021-c552674a4621

In this tutorial we will discuss how to run our first selenium test script on Chrome browser using Python.

So let’s start 👍

Here are some pre-requisites:

· Download Chrome driver

· Package to be import :

- From selenium import webdriver

You can download chrome driver for Windows, Mac and Linux from here :

👉 https://chromedriver.storage.googleapis.com/index.html?path=90.0.4430.24/

Note : For windows, only 32 bit version available but it will work for both Windows 32 and 64 bit versions.

In previous tutorial, we learnt that For every new project, we have to install the required packages every time. Like here, I installed the Selenium package once again because we are creating a new package:

First we to have create a new Project “Demo_Selenium_Project” as I did, it first create a virtual environment(Venv).

Virtual environment or venv, it creates a separate python virtual environment which contains all the dependencies for the project. It is recommended that for every project you should have a separate virtual environment so it won’t affect dependencies of other projects.

So now after creating new Project, create a Python file by right clicking on project –

Demo_Selenium_Project -> New -> Python file and give some name like this -

After creating new Python file, Let’s start by writing our first Selenium test script on Chrome browser. As I above mentioned, Please must installed the required Chrome driver from the given link before starting, without it we cannot run our test script.

Now here’s the code:

As you can see we have to import selenium first and below explain the code:

From selenium import webdriver - It provides all implementations of webdriver like Chrome, Firefox, IE etc.

from selenium import webdriver

· Driver = Webdriver.Chrome(executable_path=”path_of_ChromeDriver.exe”)

Here the instance of Chrome driver is created. We have to provide the path of Chromedriver.exe from our local system where it has been downloaded.

driver =  webdriver.Chrome(executable_path="C://Users//ayussriv//Downloads//chromedriver_win32 (1)//chromedriver.exe")

· Driver.get(https://www.google.com/) : The driver.get() method will navigate to the given page URL.

driver.get("https://www.google.com/")

· Print(driver.title) : It will print the title of the loaded page.

print(driver.title)

You can see the output page title here:

Driver.close() : It will close the current opening tab. You can also use quit() method but it will close the entire browser whereas close() exits only one tab.

driver.close()

Final step, you can run the script by two ways:

1) By clicking right button and then on “Run Filename”

2) Or simply clicking on top right corner arrow button like this:

Conclusion

In this tutorial, we learnt how you can run your first Selenium test script on Pycharm. We imported Selenium Webdriver package to use all webdriver implementations and using driver we created an instance of Chrome driver. Then loaded the URL page where we have to navigate i.e. Google.com and print the title of the page. Lastly we exit from the opening window.

Thanks for the reading to the end. Hope you enjoyed this article . 😊

--

--

Ayush Srivastava
Ayush Srivastava

Written by Ayush Srivastava

Engineer by Profession, Friendly by Nature

No responses yet