-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPropertyCheckerBot.py
301 lines (257 loc) · 10.3 KB
/
PropertyCheckerBot.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import openpyxl
import os
import time
xl_path = "Input.xlsx"
PIDsiteURL = ADRsiteURL = ""
os.environ['WDM_LOG_LEVEL'] = '0'
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
options.add_argument("--start-maximized")
options.add_argument("--disable-popups")
DEFAULT_IMPLICIT_WAIT = 10
def GetElement(driver, elementTag, locator='ID'):
'''Wait for element and then return when it is available'''
try:
if locator == 'ID':
if is_element_present(driver, By.ID, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_id(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
elif locator == 'NAME':
if is_element_present(driver, By.NAME, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_name(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
elif locator == 'XPATH':
if is_element_present(driver, By.XPATH, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_xpath(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
elif locator == 'CSS':
if is_element_present(driver, By.CSS_SELECTOR, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_css_selector(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
except Exception as e:
print('Error identifying element - %s.' % e)
return None
def is_element_present(driver, how, what):
"""Check if an element is present"""
try:
driver.find_element(by=how, value=what)
except NoSuchElementException:
return False
return True
def WaitTillElementPresent(driver, elementTag, locator='ID'):
'''Wait till element present. Max 30 seconds'''
result = False
driver.implicitly_wait(0)
for i in range(30):
try:
if locator == 'ID':
if is_element_present(driver, By.ID, elementTag):
result = True
break
elif locator == 'NAME':
if is_element_present(driver, By.NAME, elementTag):
result = True
break
elif locator == 'XPATH':
if is_element_present(driver, By.XPATH, elementTag):
result = True
break
elif locator == 'CSS':
if is_element_present(driver, By.CSS_SELECTORS, elementTag):
result = True
break
except Exception as e:
print('Exception when WaitTillElementPresent : %s' % e)
pass
time.sleep(0.99)
else:
print("Timed out. Unable to find the Element: %s" % elementTag)
driver.implicitly_wait(DEFAULT_IMPLICIT_WAIT)
return result
def Type(driver, elementTag, locator='ID', input_text=''):
"""Type the given input text"""
try:
WaitTillElementPresent(driver, elementTag, locator)
element = GetElement(driver, elementTag, locator)
element.click()
element.clear()
element.send_keys(input_text)
except Exception as e:
print('Exception when Type : %s' % e)
pass
def Click(driver, elementTag, locator='ID'):
"""Click any given element"""
try:
WaitTillElementPresent(driver, elementTag, locator)
element = GetElement(driver, elementTag, locator)
element.click()
except Exception as e:
print('Exception when Click : %s' % e)
pass
def scrollTo(driver, elementTag, locator='ID'):
"""Scroll to any given element"""
try:
element = GetElement(driver, elementTag, locator)
if element:
driver.execute_script("arguments[0].scrollIntoView({block: \"center\", inline: \"center\"});", element)
except Exception as e:
print('Exception when scrolling : %s' % e)
pass
def tearDown(driver):
"""Close the browser and driver at the end of script"""
try:
driver.close()
except Exception as e:
pass
try:
driver.quit()
except Exception as e:
pass
def is_alert_present(driver):
"""Function to find if any alert is visible"""
try:
driver.switch_to_alert
except NoAlertPresentException:
return False
return True
def close_alert(driver):
"""Function to close alert if any"""
alert_text = ''
try:
if (is_alert_present(driver)):
alert = driver.switch_to.alert
alert_text = alert.text
print("Alert Message : " + alert_text)
alert.dismiss()
if (is_alert_present(driver)):
alert = driver.switch_to.alert
print("Alert Message : " + alert.text)
alert.accept()
return alert_text
except Exception as e:
#print('Exception when Alert : %s' % e)
pass
def Found(filename):
"""
Function to find if an input file exists.
Give a file path and output will be True or False.
"""
try:
if filename is None:
print("Input Filename is None.")
return False
else:
if os.path.exists(filename) is True:
if os.path.isfile(filename):
# print('%s is available.' % os.path.basename(filename))
return True
else:
print('Error:Input Path %s is not a file but a folder.' % filename)
return False
else:
print('Error: %s Doesnot Exist. Or Path is incorrect.' % filename)
return False
except:
print('Error: %s Doesnot Exist. Or Path is incorrect.' % filename)
return False
def Closed(filename):
"""
Function to check if an input file exists and not open.
Returns True if the File is closed
Returns False if the File is Open/Not Found.
"""
if Found(filename) is True:
try:
os.rename(filename, filename)
# print('%s is Closed!' % (os.path.basename(filename)))
return True
except Exception as e:
print('Error Accessing the file! File may be Open!\n' + str(e))
return False
def main():
if not Closed(xl_path):
print('Please close the file and re-run this bot.')
else:
wb = None
try:
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
driver.maximize_window()
print("Google Chrome Launched!")
# Load the workbook from path
# Dont use_iterators=True when read_only=False. File Wont save!
wb = openpyxl.load_workbook(xl_path, read_only=False, data_only=True)
# Open the worksheet from the name
print(f"Excel {xl_path} opened. Sheets found : {wb.sheetnames}")
#Take the first sheet name
sheet = wb[wb.sheetnames[0]]
# Get Row and Column count
row_count = sheet.max_row
column_count = sheet.max_column
colNum = column_count + 1
print('No. of rows = %r and columns = %r' % (row_count, column_count))
for row in range(2, row_count + 1):
PID = sheet['C' + str(row)].value
if PID and len(PID) >= 13:
siteURL = ADRsiteURL
if "-" in PID:
p = PID.replace("-", "").strip()
if p.isnumeric():
siteURL = PIDsiteURL
driver.get(siteURL)
driver.implicitly_wait(DEFAULT_IMPLICIT_WAIT)
WaitTillElementPresent(driver, "//input", locator='XPATH')
if siteURL == PIDsiteURL:
Type(driver, "//*[@name='pid']", locator='XPATH', input_text=PID)
Click(driver, "//*[@value='Search']", locator='XPATH')
close_alert(driver)
if siteURL == ADRsiteURL:
AdressList = PID.strip().split(" ")
HOUSE = ADDRESS = UNIT = ""
if AdressList[0].strip().isnumeric():
HOUSE = AdressList[0].strip()
if "#" in AdressList[-1]:
UNIT = AdressList[-1].replace("#", "").strip()
del AdressList[-1]
ADDRESS = " ".join(AdressList[1:])
else:
HOUSE = "1"
if "#" in AdressList[-1]:
UNIT = AdressList[-1].replace("#", "").strip()
del AdressList[-1]
ADDRESS = " ".join(AdressList)
if ADDRESS:
if HOUSE:
Type(driver, "//input[@name='house']", locator='XPATH', input_text=HOUSE)
Type(driver, "//input[@name='street']", locator='XPATH', input_text=ADDRESS)
if UNIT:
Type(driver, "//input[@name='condo']", locator='XPATH', input_text=UNIT)
Click(driver, "//*[@value='Search']", locator='XPATH')
close_alert(driver)
WaitTillElementPresent(driver, "//*[contains(text(), 'Owner name')] | //*[text() = 'No records found']", locator='XPATH')
except Exception as e:
print(str(e))
finally:
tearDown(driver)
# Save the workbook.
wb.save(xl_path)
if __name__ == '__main__':
main()