How to get started with Selenium?

Prakash R
featurepreneur
Published in
3 min readAug 26, 2021

Selenium allows you to define tests and automatically detect the results of these tests on a pre-decided browser. A suite of Selenium functions enables you to create step-by-step interactions with a webpage and assess the response of a browser to various changes. You can then decide if the response of the browser is in line with what you expect.

Pre-requisites for running Selenium tests with Python:

The easiest way to install Selenium on a Python environment is through the installer pip.

pip install selenium

While the installation of Selenium makes the functionality available to you, you need additional drivers for it to be able to interface with a chosen web browser. The download links for the drivers are available here: Chrome, Edge, Firefox, and Safari.

How to run your automated test using Selenium and Python?

Once you have completed the pre-requisites section, you are ready to start your first test in Selenium with the Python programming language!

1. First import the webdriver and Keys classes from Selenium.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

2. Next, create an instance of Chrome with the path of the driver that you downloaded through the websites of the respective browser. In this example, we assume that the driver is in the same directory as the Python script that you will execute.

driver = webdriver.Chrome('./chromedriver')

3. Next, use the .get() method of the driver to load a website. You may also load a local development site as this process is equivalent to opening a window of Chrome on your local machine, typing a URL and hitting Enter. The .get() method not only starts loading a website but also waits for it to render completely before moving on to the next step.

driver.get("https://www.python.org")

4. Once the page loads successfully, you can use the .title attribute to access the textual title of the webpage. If you wish to check whether the title contains a particular substring, you can use the assert or if statements. For simplicity, let us print the title of the page.

print(driver.title)

The output is the following text –

Welcome to Python.org

5. Next, let us submit a query in the search bar. First, select the element from the HTML DOM and enter a value into it and submit the form by emulating the Return key press. You can select the element using its CSS class, ID, its name attribute, or even the tag name. If you check the source of the query search bar, you notice that the name attribute of this DOM element is “q”. Therefore, you can use the .find_element_by_name() method as follows to select the element.

search_bar = driver.find_element_by_name("q")

6. Once the DOM element is selected, you first need to clear its contents using the .clear() method, enter a string as its value using the .send_keys() method and finally, emulate the press of the Return key using Keys.RETURN.

search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)

To close the current session, use the .close() method. It also disconnects the link with the browser.

driver.close()

The entire code looks like:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()

--

--