The purpose of this tutorial is to introduce you to Selenium and show the process of installing selenium and webdriver for browser automation. In this article, we’ve assumed that you already have python installed on your machine.
It’s an important point to note that Browser automation and Web Scrapping are completely White Hat in their approach and the Web Browsers themselves support it officially, provide web drivers for automation and testing, just the browser window opened using code is labeled as “this browser is being controlled by automated test software”
What is Selenium?
Selenium is an open-source project which provides a wide range of tools to automate the web browser. It is also used to create web scrapers to obtain(scrape) desired data from web pages.
Some tasks that can be done using Python Selenium are:
- Automating Browser tasks such as login, meeting join, scrolling, surfing, etc.
- Getting Data from Website/webpage in text, excel file, code, etc.
A crucial component in browser automation is a Webdriver. A Webdriver is a collection of APIs which makes interaction with the browser easy. Combining Selenium and webdriver make it very easy to automate boring tasks of the web.
To start with setting up our computer for browser automation and web scraping, we need to start with the installation of some tools and libraries.
1. Install Selenium
First we’ll install the selenium package using pip. It is very easy to install any python package using the pip install package_name command.
Open the command prompt of your computer and enter the below command. You can also run the command in the terminal of your system or IDE.
pip install selenium
It will install the latest version of selenium on our machine.
2. Install the Selenium Webdriver
Our second task, in setting up is to install the webdriver as per our browser, which we intend to use to automate.
While installing the webdriver we need to make sure that it has the same version as our Web Browser. Every browser has its own webdriver which is maintained by its parent companies.
Below are the links for downloading webdriver of the popular Web Browers – Mozilla Firefox, Google Chrome, and Microsoft Edge respectively.
After downloading selenium and the required webdriver, you are ready to write python scripts to automate the web browser.
3. Importing Selenium in Python
As we have downloaded the required tools and libraries, as a final step we need to import the required as follow:
Note: We need to pass the location (as saved on our computer) of the installed web driver file to the webdriver method.
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome('C://software/chromedriver.exe')
Tip: Instead, of passing the location as a parameter every time, in every code, we can skip this step in a smart way by declaring (saving) the location of the downloaded web driver file as an environment variable.
Recommended Reads for Selenium
Finally, we are done with the setup and you may start web scraping and automating your web browser tasks by following our tutorials on Python Selenium –
- Open Web Page URLs using Python Selenium
- Fetch data from a web page
- Fetch Stock Market data using Selenium