Python – Installation (Selenium and Chrome Driver), Open URL, and Basic Search

This is an introduction to Selenium and includes installation of Selenium and a driver (in this case ChromeDriver) as well as some basic use cases for Selenium. Selenium is primarily used for writing test classes for web applications.

To install or upgrade selenium run you command prompt as an administrator (for PC shift + right click and select “run as administrator”:

pip install -U selenium

You should see the final result (your version number may be different):

Successfully installed selenium-3.9.0

Selenium requires a driver to interface with the chosen browser. Make sure it’s in your PATH, e. g., place it in /usr/bin or /usr/local/bin.

Failure to observe this step will give you an error selenium.common.exceptions.WebDriverException: Message: ‘ChromDriver’ executable needs to be in PATH.

See links to popular drivers below:


I will be using the chrome driver 2.35 with windows 10.

Chrome driver is a standalone server that implements WebDriver’s wire protocol.

It consists of three separate pieces (1) the browser itself (chrome), (2) the language bindings provided by the selenium project (the driver), (3) and an executable downloaded from the chromium project which acts as a bridge between “chrome” and the “driver”.

See for reference:

Open the zip file and run the .exe file.

The command prompt will open and you will see that the chrome driver (version denoted) has started at your port with only local connections allowed.

Extract the zip file to a directory in your PATH or create a new PATH to the chromedriver executable. I added chrome.exe to my root directory ( C:\ )

Verify in the command prompt that you placed the executable appropriately by typing: chromedriver

This should return the same result in the command prompt as if you had clicked the executable file directly.

Here is a test you can perform:

import time

from selenium import webdriver
import as service

service = service.Service('C:\chromedriver.exe')
capabilities = {'chrome.binary': 'C:\chromedriver.exe'}
driver = webdriver.Remote(service.service_url, capabilities)
time.sleep(10) # Let the user actually see something!

Here is an example of performing a search:

#module for webdriver implementations for in this case chrome
from selenium import webdriver
#Keys provides keyboard commands like Return, F1, Alt, etc
from selenium.webdriver.common.keys import Keys

#creates an instance of chrome driver
driver = webdriver.Chrome()

#returns the URL in the browser

#confirm that title has python in it
assert "Python" in driver.title

#searches for the element "q" in webpage source code
#see source code on webpage by going to chrome inspect
elem = driver.find_element_by_name("q")

#to be safe clear any values in the prepopulated text search
#search for pycon
#keyboard command to press enter

#used to ensure that some results are found
assert "No result found." not in driver.page_source

# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
# you can find the class name by looking at the elements in chrome inspect
lists= driver.find_elements_by_class_name("single-event-date")

# get the number of elements found
print ("Found " + str(len(lists)) + " searches:")

# iterate through each element and print the text that is
# name of the search
for listitem in lists:
 print (listitem)

#quit the browser

When you run the script you will see that “Chrome is being controlled by test software” and the browser will navigate to Then it will do a search for “pycon”.


Locating Elements in Selenium >>

%d bloggers like this: