-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (44 loc) · 1.63 KB
/
app.py
File metadata and controls
51 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pytesseract import image_to_string
import pytesseract
from PIL import Image
from selenium import webdriver
from flask import Flask
app = Flask(__name__)
url = 'https://esearchigr.maharashtra.gov.in/portal/esearchlogin.aspx'
@app.route("/")
def home():
return '<a href="/start/captcha/">Click here to run automation.</a>'
@app.route("/start/captcha/")
def login_to_website():
driver = webdriver.Chrome(executable_path="./chromedriver")
driver.get(url)
# driver.set_window_size(1120, 550)
#find part of the page you want image of
element = driver.find_element_by_xpath('//*[@id="form1"]/div[3]/div[4]/div[2]/div[2]/div/table/tbody/tr[3]/td[1]/img')
location = element.location
size = element.size
driver.save_screenshot('screenshot.png')
user_id = driver.find_element_by_xpath('//*[@id="txtUserid"]')
user_id.clear()
user_id.send_keys('your-username')
password = driver.find_element_by_xpath('//*[@id="txtPswd"]')
password.clear()
password.send_keys('your-password')
captcha = driver.find_element_by_xpath('//*[@id="txtcaptcha"]')
captcha.clear()
captcha_text = get_captcha_text(location, size)
captcha.send_keys(captcha_text)
driver.find_element_by_xpath('//*[@id="btnLogin"]').click()
def get_captcha_text(location, size):
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
im = Image.open('screenshot.png')
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom))
im.save('screenshot.png')
captcha_text = image_to_string(Image.open('screenshot.png'))
return captcha_text
if __name__ == '__main__':
app.run(5000)