-
Notifications
You must be signed in to change notification settings - Fork 0
/
naukri.py
executable file
·174 lines (145 loc) · 6.31 KB
/
naukri.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
#! python3
# Naukri Daily update - Using firefox
import inspect
import os
import sys
import time
import logging
from selenium import webdriver
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.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
# Update your username, password and mobile number before start
username = ""
password = ""
mob = ""
naukri_url = "https://login.naukri.com/nLogin/Login.php"
profile_url = 'https://www.naukri.com/mnjuser/profile'
current_dr = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
log_file_path = os.path.join(current_dr, 'naukri.log')
driver_log_path=os.path.join(current_dr, "geckodriver.log")
logging.basicConfig(level=logging.INFO,
filename=log_file_path,
format='%(asctime)s : %(message)s')
def is_element_present(driver, how, what):
try:
driver.find_element(by=how, value=what)
except NoSuchElementException:
return False
return True
def tear_down(driver):
try:
driver.close()
logging.info('Driver Closed Successfully')
driver.quit()
logging.info('Driver Quit Successfully')
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
line_no = str(exc_tb.tb_lineno)
logging.info('Error quitting: %s : %s at Line %s.' % (type(e), e, line_no))
print('Error quitting: %s : %s at Line %s.' % (type(e), e, line_no))
def naukri_login():
status = False
try:
options = Options()
options.headless = True
options.add_argument('window-size=1200x1040')
options.add_argument("--disable-notifications")
options.add_argument("--start-maximized")
options.add_argument("--disable-popups")
driver = webdriver.Firefox(options=options, service_log_path=driver_log_path)
driver.maximize_window()
print("Firefox Launched!")
logging.info("Firefox Launched!")
driver.implicitly_wait(3)
driver.get(naukri_url)
if 'Naukri.com' in driver.title:
print("We are in")
logging.info("We are in")
if is_element_present(driver, By.ID, "emailTxt"):
email_field_element = driver.find_element_by_id("emailTxt")
time.sleep(1)
pass_field_element = driver.find_element_by_id("pwd1")
time.sleep(1)
login_button = driver.find_element_by_xpath("//*[@type='submit' and @value='Login']")
elif is_element_present(driver, By.ID, "usernameField"):
email_field_element = driver.find_element_by_id("usernameField")
time.sleep(1)
pass_field_element = driver.find_element_by_id("passwordField")
time.sleep(1)
login_button = driver.find_element_by_xpath('//*[@type="submit"]')
else:
print('Look like we don\'t have an element to login')
logging.info('Look like we don\'t have an element to login')
if email_field_element is not None:
email_field_element.clear()
email_field_element.send_keys(username)
pass_field_element.clear()
pass_field_element.send_keys(password)
time.sleep(1)
login_button.send_keys(Keys.ENTER)
result = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'search-jobs')))
if result:
print('Logged in')
logging.info('Logged in')
status = True
return (status, driver)
else:
print('Something went wrong while trying to login')
logging.info('Something went wrong while trying to login')
return (status, driver)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
line_no = str(exc_tb.tb_lineno)
logging.info('Error while logging in to account: %s : %s at line %s.\n' % (type(e), e, line_no))
print('Error while logging in to account: %s : %s at line %s.' % (type(e), e, line_no))
return (status, driver)
def update_profile(driver):
try:
driver.get(profile_url)
driver.implicitly_wait(10)
time.sleep(3)
mob_xpath = '//*[@id="mob_number"]'
profedit_xpath = "/html/body/div[2]/div/div[1]/span/div/div/div/div/div/div[1]/div[1]/div/div[1]/div/div[2]/div[1]/div/div[1]/em"
save_xpath = '//*[@id="saveBasicDetailsBtn"]'
element = driver.find_element_by_xpath(profedit_xpath)
driver.execute_script("arguments[0].click();", element)
time.sleep(4)
if is_element_present(driver, By.XPATH, save_xpath):
mob_field_element = driver.find_element_by_xpath(mob_xpath)
mob_field_element.clear()
mob_field_element.send_keys(mob)
driver.implicitly_wait(2)
save_field_element = driver.find_element_by_xpath(save_xpath)
save_field_element.send_keys(Keys.ENTER)
driver.implicitly_wait(3)
if is_element_present(driver, By.XPATH, "//*[text()='today']"):
logging.info('Mobile number updated successfully')
print('Mobile number updated successfully')
else:
logging.info('Mobile number updation failed')
print('Mobile number updation failed')
time.sleep(5)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
line_no = str(exc_tb.tb_lineno)
logging.info('Error updating mobile number: %s : %s at Line %s.\n' % (type(e), e, line_no))
print('Error updating mobile number: %s : %s at Line %s.' % (type(e), e, line_no))
def main():
driver = None
try:
status, driver = naukri_login()
if status:
update_profile(driver)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
line_no = str(exc_tb.tb_lineno)
logging.info('Error updating profile: %s : %s at Line %s.' % (type(e), e, line_no))
print('Error updating profile: %s : %s at Line %s.' % (type(e), e, line_no))
finally:
tear_down(driver)
if __name__ == '__main__':
main()