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:

Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox: https://github.com/mozilla/geckodriver/releases
Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

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: https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver

screenshot-sites.google.com-2018.02.19-06-46-56

screenshot-chromedriver.storage.googleapis.com-2018.02.19-06-48-41

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 selenium.webdriver.chrome.service as service

service = service.Service('C:\chromedriver.exe')
service.start()
capabilities = {'chrome.binary': 'C:\chromedriver.exe'}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://metaspace.blog/programming/python')
time.sleep(10) # Let the user actually see something!
driver.quit()

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
driver.get("http://www.python.org")

#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
elem.clear()
#search for pycon
elem.send_keys("pycon")
#keyboard command to press enter
elem.send_keys(Keys.RETURN)

#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
i=0
for listitem in lists:
 print (listitem)
 i=i+1
 if(i>10):
 break

time.sleep(10)
#quit the browser
driver.close()

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

 

Locating Elements in Selenium >>