Selenium Hello World with Python

By xngo on February 20, 2019

Overview

Selenium is a tool that take control over the browsers and simulate web surfing activities. Therefore, it is able to replicate any action typically undertaken by a user, such as clicking, typing and retrieving data from web pages. I will show you how to setup and run Selenium using Python.

Setup

# Install the python package manager, pip.
    apt-get install python-pip
 
# Install Python bindings for Selenium.
    pip install selenium
 
# Download Webdriver for Chrome from 
#   <https://sites.google.com/a/chromium.org/chromedriver/downloads>. 
    unzip chromedriver_linux64.zip
 

Run Selenium

  1. Close all instances of your Chrome browser.
  2. Run ./chromedriver on 1 terminal and then run the Python code below from another terminal.
#!/usr/bin/python3
# Description: The Python code below will search selenium in Google.
 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
 
driver = webdriver.Chrome()
driver.get("https://www.google.com")
 
search_box = driver.find_element_by_name('q')  # Find search input box.
search_box.send_keys('selenium')               # Type in selenium.
search_box.send_keys(Keys.RETURN)              # Press ENTER.
 
# Close.
#driver.close()

Here is a video showing the code above in action.

Video showing Selenium in action

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.