Web Automation Using Selenium WebDriver(Chrome) + Python3(Bypass Captcha)

Kaushal Agarwal
3 min readApr 5, 2019

--

OS-Requirement

Linux Platform(Ubuntu Recommended)

Pre-Installed Requirements

  1. Python3
  2. Google-Chrome Browser
  3. https://github.com/kaushalag29/BIT-Erp-Automated.git (Git-Clone/Download This Repo)
  4. Download Chrome-Driver Based On Your Google-Chrome Version As Shown In Below Pic For Linux(Zip File) From https://chromedriver.storage.googleapis.com/index.html
Compatibility Check

Pre-Requisites

  1. Basic Knowledge And Syntax Of Python.
  2. Basic Knowledge Of HTML
  3. Using Inspect Element Feature In Chrome Browser

Steps To Set-Up Downloaded Git Repo (Demo)

Note:- Please try to run this demo before proceeding further so that you have all the requirements installed to execute your own code in the future.Even if you don’t have an account,just make sure that after executing the script,the browser is opening automatically,i.e, the chrome-driver supports your browser.

  1. Copy the downloaded Chrome-Driver Zip file in downloaded/cloned Git repo directory(BIT-Erp-Automated).
  2. Unzip Chrome-Driver Zip file to get the executable binary file(set “chromedriver” as name of file if not already) of chrome-driver and delete the zip file.
  3. Execute “installer.py”(Located in Git-Repo Dir)file using terminal command as shown :- python3 installer.py
  4. Change the credentials(Username,Password) in “Erp.py”(Located in Git-Repo Dir) file.(#For Testing Skip This Step But Note That The Output Will Not Be Same As In Step 6 )
  5. Execute “Erp.py”(Located in Git-Repo Dir) file using terminal command as shown :- python3 Erp.py
  6. Attendance.html file will be generated in the same repo directory at the end of execution of script which is the required output in this case.

Goal

Attempting to automate the login process of a website that requires/not-requires captcha verification.

Idea

We will have variables in code for storing username,password(credentials required for login page of website) and we can get captcha text from its image url.

We can get image url by inspecting element on the image displayed in chrome browser.Then we can download the image first and then can use offline OCR to extract captcha-text from image and store it in a variable.

Let’s Stick To Code Now

#Assuming the Code file is located in Git-repo Directory

#Please go through the code as well as comments

from selenium import webdriver #For Selenium Web Driver
import os #For executing terminal commands as well as accessing directory
import requests #For Session

url = "http://erp.bitmesra.ac.in" #Target Url
captcha_image_url = url + "/GenerateCaptcha.ashx" #This url is known by doing inspect element on captcha_image generated

#Store Credentials
username = "Your-Username"
Password = "Your Password"

browser = webdriver.Chrome(executable_path=os.curdir + '/chromedriver') #Loading Browser Instance
#Note the binary_file_name is important in above line
#os.curdir gives the current directory location in which your code file is situated

browser.get(url) #Opening Target Url

# Creating Session so that same captcha image is loaded on opening the captcha_url as well as target_url
cookies = browser.get_cookies()
session = requests.Session()
for cookie in cookies:
session.cookies.set(cookie['name'], cookie['value'])

# Downloading Image Captcha("captcha.jpeg" name is given to file)
try:
read = session.get(captcha_url)
with open("captcha.jpeg", 'wb') as w:
for chunk in read.iter_content(chunk_size=512):
if chunk:
w.write(chunk)
except:
print("Error Downloading Captcha")
browser.quit()

os.system("cp captcha.jpeg " + os.curdir + "/ocr-convert-image-to-text")
print("File Copied")

# Using OCR-Tool to convert image to text
os.chdir(os.curdir + "/ocr-convert-image-to-text")
os.system("python3 main.py --input_dir captcha.jpeg --output_dir .")
print("captcha.txt generated")

# Reading captcha from file
with open("captcha.txt", "r") as captcha_file:
for line in captcha_file:
captcha_text = line.strip()
break

print("Captcha = " + captcha_text)


#All the elements refer to html elements in html code and the name(unique identifier) of each tag used in html code
#Selecting Form Fields
username_element = browser.find_element_by_name('txt_username')
password_element = browser.find_element_by_name('txt_password')
captcha_element = browser.find_element_by_name('txtcaptcha')
submit_btn = browser.find_element_by_id('btnSubmit')

# Filling up Form by credentials gathered
username_element.send_keys(username)
password_element.send_keys(password)
captcha_element.send_keys(captcha_text)

# Submitting Form
submit_btn.click()

#That's it in this way you can automate the login process.

Limitation

OCR Tool Not Always Extracts Correct Text From Image.(You can fix this issue by recursively attempting to login unless the captcha matches)

Alteranative

You can also use online OCR Tool As per Requirement.It will require more efforts and internet data,so i avoided this technique.

That’s All.You are good to go.Please do clap if you like this post.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kaushal Agarwal
Kaushal Agarwal

No responses yet

Write a response