Skip to content

Commit

Permalink
Minor updates to fix recent changes to Naukri
Browse files Browse the repository at this point in the history
  • Loading branch information
navchandar authored Apr 11, 2024
1 parent 3d95478 commit 77f763e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 61 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Install [Python 3.10+](https://www.python.org/getit/) and run the below commands
git clone https://github.com/navchandar/Naukri.git
cd Naukri
pip install --upgrade pip
python3 -m venv .venv # create virtual environment for installing dependencies
./.venv/bin/activate.ps1 # or source ./.venv/bin/activate for macOS/linux
pip install -r requirements.txt
```

Expand All @@ -32,7 +34,7 @@ python naukri.py

| [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Chrome |
| --------- |
| last 20 versions
| last 10 versions


## Contribute
Expand Down
128 changes: 68 additions & 60 deletions naukri.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
# -*- coding: utf-8 -*-
"""Naukri Daily update - Using Chrome"""

import re
import os
import io
import logging
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.common.exceptions import NoAlertPresentException
from pypdf import PdfReader, PdfWriter
from string import ascii_uppercase, digits
from random import choice, randint
from datetime import datetime
from reportlab.pdfgen import canvas
from random import choice, randint
from string import ascii_uppercase, digits

from pypdf import PdfReader, PdfWriter
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager as CM


# Add folder Path of your resume
originalResumePath = "original_resume.pdf"
# Add Path where modified resume should be saved
Expand All @@ -45,7 +44,7 @@
level=logging.INFO, filename="naukri.log", format="%(asctime)s : %(message)s"
)
# logging.disable(logging.CRITICAL)
os.environ['WDM_LOCAL'] = "1"
os.environ["WDM_LOCAL"] = "1"
os.environ["WDM_LOG_LEVEL"] = "0"


Expand All @@ -57,7 +56,7 @@ def log_msg(message):

def catch(error):
"""Method to catch errors and log error details"""
exc_type, exc_obj, exc_tb = sys.exc_info()
_, _, exc_tb = sys.exc_info()
lineNo = str(exc_tb.tb_lineno)
msg = "%s : %s at Line %s." % (type(error), error, lineNo)
print(msg)
Expand All @@ -67,25 +66,27 @@ def catch(error):
def getObj(locatorType):
"""This map defines how elements are identified"""
map = {
"ID" : By.ID,
"NAME" : By.NAME,
"XPATH" : By.XPATH,
"TAG" : By.TAG_NAME,
"CLASS" : By.CLASS_NAME,
"CSS" : By.CSS_SELECTOR,
"LINKTEXT" : By.LINK_TEXT
"ID": By.ID,
"NAME": By.NAME,
"XPATH": By.XPATH,
"TAG": By.TAG_NAME,
"CLASS": By.CLASS_NAME,
"CSS": By.CSS_SELECTOR,
"LINKTEXT": By.LINK_TEXT,
}
return map[locatorType.upper()]


def GetElement(driver, elementTag, locator="ID"):
"""Wait max 15 secs for element and then select when it is available"""
try:

def _get_element(_tag, _locator):
_by = getObj(_locator)
if is_element_present(driver, _by, _tag):
return WebDriverWait(driver, 15).until(
lambda d: driver.find_element(_by, _tag))
lambda d: driver.find_element(_by, _tag)
)

element = _get_element(elementTag, locator.upper())
if element:
Expand Down Expand Up @@ -113,14 +114,14 @@ def WaitTillElementPresent(driver, elementTag, locator="ID", timeout=30):
driver.implicitly_wait(0)
locator = locator.upper()

for i in range(timeout):
for _ in range(timeout):
time.sleep(0.99)
try:
if is_element_present(driver, getObj(locator), elementTag):
result = True
break
except Exception as e:
log_msg('Exception when WaitTillElementPresent : %s' %e)
log_msg("Exception when WaitTillElementPresent : %s" % e)
pass

if not result:
Expand Down Expand Up @@ -163,20 +164,24 @@ def LoadNaukri(headless):
# updated to use ChromeDriverManager to match correct chromedriver automatically
driver = None
try:
driver = webdriver.Chrome(executable_path=CM().install(), options=options)
driver = webdriver.Chrome(options, service=ChromeService(CM().install()))
except:
driver = webdriver.Chrome(options=options)
driver = webdriver.Chrome(options)
log_msg("Google Chrome Launched!")

driver.implicitly_wait(3)
driver.get(NaukriURL)
return driver


def naukriLogin(headless = False):
""" Open Chrome browser and Login to Naukri.com"""
def naukriLogin(headless=False):
"""Open Chrome browser and Login to Naukri.com"""
status = False
driver = None
username_locator = "usernameField"
password_locator = "passwordField"
login_btn_locator = "//*[@type='submit' and normalize-space()='Login']"
skip_locator = "//*[text() = 'SKIP AND CONTINUE']"

try:
driver = LoadNaukri(headless)
Expand All @@ -185,22 +190,12 @@ def naukriLogin(headless = False):
log_msg("Website Loaded Successfully.")

emailFieldElement = None
if is_element_present(driver, By.ID, "emailTxt"):
emailFieldElement = GetElement(driver, "emailTxt", locator="ID")
if is_element_present(driver, By.ID, username_locator):
emailFieldElement = GetElement(driver, username_locator, locator="ID")
time.sleep(1)
passFieldElement = GetElement(driver, "pwd1", locator="ID")
passFieldElement = GetElement(driver, password_locator, locator="ID")
time.sleep(1)
loginXpath = "//*[@type='submit' and @value='Login']"
loginButton = GetElement(driver, loginXpath, locator="XPATH")

elif is_element_present(driver, By.ID, "usernameField"):
emailFieldElement = GetElement(driver, "usernameField", locator="ID")
time.sleep(1)
passFieldElement = GetElement(driver, "passwordField", locator="ID")
time.sleep(1)
loginXpath = '//*[@type="submit"]'
loginButton = GetElement(driver, loginXpath, locator="XPATH")

loginButton = GetElement(driver, login_btn_locator, locator="XPATH")
else:
log_msg("None of the elements found to login.")

Expand All @@ -216,13 +211,13 @@ def naukriLogin(headless = False):

# Added click to Skip button
print("Checking Skip button")
skipAdXpath = "//*[text() = 'SKIP AND CONTINUE']"
if WaitTillElementPresent(driver, skipAdXpath, locator="XPATH", timeout=10):
GetElement(driver, skipAdXpath, locator="XPATH").click()

if WaitTillElementPresent(driver, skip_locator, "XPATH", 10):
GetElement(driver, skip_locator, "XPATH").click()

# CheckPoint to verify login
if WaitTillElementPresent(driver, "search-jobs", locator="ID", timeout=40):
CheckPoint = GetElement(driver, "search-jobs", locator="ID")
if WaitTillElementPresent(driver, "ff-inventory", locator="ID", timeout=40):
CheckPoint = GetElement(driver, "ff-inventory", locator="ID")
if CheckPoint:
log_msg("Naukri Login Successful")
status = True
Expand All @@ -242,18 +237,24 @@ def naukriLogin(headless = False):
def UpdateProfile(driver):
try:
mobXpath = "//*[@name='mobile'] | //*[@id='mob_number']"
profeditXpath = "//a[contains(text(), 'UPDATE PROFILE')] | //a[contains(text(), ' Snapshot')] | //a[contains(@href, 'profile') and contains(@href, 'home')]"
saveXpath = "//button[@ type='submit'][@value='Save Changes'] | //*[@id='saveBasicDetailsBtn']"
editXpath = "//em[text()='Edit']"
view_profile_locator = "//*[contains(@class, 'view-profile')]//a"
edit_locator = "(//*[contains(@class, 'icon edit')])[1]"
save_confirm = "//*[text()='today' or text()='Today']"
close_locator = "//*[contains(@class, 'crossIcon')]"

WaitTillElementPresent(driver, profeditXpath, "XPATH", 20)
profElement = GetElement(driver, profeditXpath, locator="XPATH")
WaitTillElementPresent(driver, view_profile_locator, "XPATH", 20)
profElement = GetElement(driver, view_profile_locator, locator="XPATH")
profElement.click()
driver.implicitly_wait(2)

WaitTillElementPresent(driver, editXpath + " | " + saveXpath, "XPATH", 20)
if is_element_present(driver, By.XPATH, editXpath):
editElement = GetElement(driver, editXpath, locator="XPATH")
if WaitTillElementPresent(driver, close_locator, "XPATH", 10):
GetElement(driver, close_locator, locator="XPATH").click()
time.sleep(2)

WaitTillElementPresent(driver, edit_locator + " | " + saveXpath, "XPATH", 20)
if is_element_present(driver, By.XPATH, edit_locator):
editElement = GetElement(driver, edit_locator, locator="XPATH")
editElement.click()

WaitTillElementPresent(driver, mobXpath, "XPATH", 20)
Expand All @@ -266,8 +267,8 @@ def UpdateProfile(driver):
saveFieldElement.send_keys(Keys.ENTER)
driver.implicitly_wait(3)

WaitTillElementPresent(driver, "//*[text()='today']", "XPATH", 10)
if is_element_present(driver, By.XPATH, "//*[text()='today']"):
WaitTillElementPresent(driver, save_confirm, "XPATH", 10)
if is_element_present(driver, By.XPATH, save_confirm):
log_msg("Profile Update Successful")
else:
log_msg("Profile Update Failed")
Expand Down Expand Up @@ -298,8 +299,8 @@ def UpdateResume():
try:
# random text with with random location and size
txt = randomText()
xloc = random.randint(700, 1000) # this ensures that text is 'out of page'
fsize = random.randint(1, 10)
xloc = randint(700, 1000) # this ensures that text is 'out of page'
fsize = randint(1, 10)

packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
Expand Down Expand Up @@ -337,8 +338,15 @@ def UploadResume(driver, resumePath):
attachCVID = "attachCV"
CheckPointXpath = "//*[contains(@class, 'updateOn')]"
saveXpath = "//button[@type='button']"
close_locator = "//*[contains(@class, 'crossIcon')]"

driver.get("https://www.naukri.com/mnjuser/profile")

time.sleep(2)
if WaitTillElementPresent(driver, close_locator, "XPATH", 10):
GetElement(driver, close_locator, locator="XPATH").click()
time.sleep(2)

WaitTillElementPresent(driver, attachCVID, locator="ID", timeout=10)
AttachElement = GetElement(driver, attachCVID, locator="ID")
AttachElement.send_keys(resumePath)
Expand Down

0 comments on commit 77f763e

Please sign in to comment.