-
Notifications
You must be signed in to change notification settings - Fork 0
/
getcaptchaimg.py
136 lines (125 loc) · 5.19 KB
/
getcaptchaimg.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from bs4 import BeautifulSoup
# from slcm import user_name, pass_word
import requests
import time
from PIL import Image
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import pytesseract as tess
from PIL import Image
tess.pytesseract.tesseract_cmd = r'D:\TESSERACT\tesseract.exe'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4.4147.135 Safari/537.36'}
baseurl = 'https://slcm.manipal.edu/'
url = "https://slcm.manipal.edu/loginForm.aspx"
resp = requests.get(url, headers=headers)
chrome_options = Options()
# chrome_options.add_argument('--window-size=800,800')
chrome_options.add_argument('headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(
ChromeDriverManager().install(), chrome_options=chrome_options)
str = ''
def get_capt():
driver.maximize_window()
s = BeautifulSoup(resp.content, features="lxml")
images = s.find('img', {'id': 'imgCaptcha'})['src']
captchaurl = baseurl + images
print(captchaurl)
driver.execute_script("window.open()")
driver.switch_to_window(driver.window_handles[1])
driver.get(captchaurl)
# time.sleep(2)
# find part of the page you want image of
element = driver.find_element_by_xpath('/html/body/img')
location = element.location
size = element.size
driver.save_screenshot('screenshot.png')
# uses PIL library to open image in memory
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 = im.convert('L') # defines crop points
im.save('screenshot.png')
img = Image.open('screenshot.png')
captcha_text = tess.image_to_string(img).strip()
print(captcha_text)
driver.close()
driver.switch_to.window(driver.window_handles[0])
return captcha_text
def login_to_website(username=None, password=None):
driver.maximize_window()
# time.sleep(2)
driver.get(url)
captcha_text = get_capt()
MarksHTML = None
AttendanceHTML = None
calendarHTML = None
user_id = driver.find_element_by_xpath('//*[@id="txtUserid"]')
user_id.clear()
user_id.send_keys(username)
passwordfinal = driver.find_element_by_xpath('//*[@id="txtpassword"]')
passwordfinal.clear()
passwordfinal.send_keys(password)
captcha = driver.find_element_by_xpath('//*[@id="txtCaptcha"]')
captcha.clear()
captcha.send_keys(captcha_text)
# time.sleep(10)
driver.find_element_by_xpath('//*[@id="btnLogin"]').click()
time.sleep(1)
if (driver.current_url == 'https://slcm.manipal.edu/studenthomepage.aspx'):
print("Logged In")
# Find Attendance Table
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.ID, 'rtpchkMenu_lnkbtn2_1')))
element.click()
driver.find_element_by_xpath('//a[@href="#3"]').click()
driver.find_element_by_xpath(
'//*[@id = "ContentPlaceHolder1_btnsearch"]').click()
time.sleep(3)
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.ID, 'tblAttendancePercentage')))
AttendanceHTML = element.get_attribute('outerHTML')
with open('attendance.html', 'wb') as f:
f.write(AttendanceHTML.encode('utf-8'))
driver.find_element_by_xpath('//a[@href="#4"]').click()
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.ID, 'PrintInternal')))
MarksHTML = element.get_attribute('outerHTML')
with open('marks.html', 'wb') as f:
f.write(MarksHTML.encode('utf-8'))
# WebDriverWait(driver,3)
driver.get('https://slcm.manipal.edu/EventCalendar.aspx')
try:
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.ID, 'ContentPlaceHolder1_divAllRep')))
except StaleElementReferenceException as e:
raise e
calendarHTML = element.get_attribute('outerHTML')
with open('calendar.html', 'wb') as f:
f.write(calendarHTML.encode('utf-8'))
page = driver.page_source
with open('homepage.html', 'wb') as f:
f.write(page.encode('utf-8'))
# f.close()
else:
print("Invalid Username/Password/Captcha")
login_to_website(username, password)
if __name__ == "__main__":
name = login_to_website("username", "password")