Home > Software design >  Job Applying Bot in Python facing IndexError: list index out of range
Job Applying Bot in Python facing IndexError: list index out of range

Time:03-05

Disclaimer: I'm coming back to scripting after more than a decade (and I was a novice to begin with) so apologies if the question is mundane but help is much needed and appreciated.

I'm trying to modify a python script to log me into a job portal, search job vacancies based on attributes and then apply to said jobs.

Since the portal opens new vacancies on separate tabs, the code is supposed to go to the next tab and test the criteria on the job description

The code snippet is as below:

for i in range(1,6):
    driver.get('https://www.naukri.com/' role '-jobs-in-' location '-' str(i) '?ctcFilter=' str(LL) 'to' str(UL))
    
driver.switch_to.window(driver.window_handles[1])
url = driver.current_url
driver.get(url)

try:
            test = driver.find_element_by_xpath('//*[@id="root"]/main/div[2]/div[2]/section[2]')
            if all(word in test.text.lower() for word in Skillset):
                driver.find_element_by_xpath('//*[@id="root"]/main/div[2]/div[2]/section[1]/div[1]/div[3]/div/button[2]').click()
                time.sleep(2)
                driver.close()
                driver.switch_to.window(driver.window_handles[0])
            else:
                driver.close()
                driver.switch_to.window(driver.window_handles[0])
except:
            driver.close()
            driver.switch_to.window(driver.window_handles[0])

However, when I run the script, it just logs me in to the portal and goes to the correct listings page but just stays there. Plus, it pops this error:

> line 43, in <module> driver.switch_to.window(driver.window_handles[1])
> IndexError: list index out of range

Not able to understand what list this is referring to and how to fix this code. Any help is appreciated.

The complete code for those interested:

import selenium
from selenium import webdriver as wb
import pandas as pd
import time
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


driver = wb.Chrome("/Users/shashank/Desktop/Naukri Auto Apply/chromedriver")


Skillset = ['Marketing','Communications','Sales']
Skillset = [x.lower() for x in Skillset]
LL = 15       #lower limit of expected CTC
UL = 25       #upper limit of expected CTC
location = 'Bangalore'
location = location.lower().replace(" ","-")
role = 'Marketing Manager'
role = role.lower().replace(" ","-")

driver.get("https://www.naukri.com")
driver.find_element_by_xpath('//*[@id="login_Layer"]/div').click()
time.sleep(5)
driver.find_element_by_xpath('//*[@id="root"]/div[3]/div[2]/div/form/div[2]/input').send_keys("email_address_here")
driver.find_element_by_xpath('//*[@id="root"]/div[3]/div[2]/div/form/div[3]/input').send_keys("password_here")
time.sleep(5)
driver.find_element_by_xpath('//*[@id="root"]/div[3]/div[2]/div/form/div[6]/button').click()

time.sleep(20)
driver.find_element_by_xpath('/html/body/div[3]/div/div[1]/div[1]/div').click()

for i in range(1,6):
    driver.get('https://www.naukri.com/' role '-jobs-in-' location '-' str(i) '?ctcFilter=' str(LL) 'to' str(UL))
    
driver.switch_to.window(driver.window_handles[1])
url = driver.current_url
driver.get(url)

try:
            test = driver.find_element_by_xpath('//*[@id="root"]/main/div[2]/div[2]/section[2]')
            if all(word in test.text.lower() for word in Skillset):
                driver.find_element_by_xpath('//*[@id="root"]/main/div[2]/div[2]/section[1]/div[1]/div[3]/div/button[2]').click()
                time.sleep(2)
                driver.close()
                driver.switch_to.window(driver.window_handles[0])
            else:
                driver.close()
                driver.switch_to.window(driver.window_handles[0])
except:
            driver.close()
            driver.switch_to.window(driver.window_handles[0])

Thanks in advance for answering! It will really help with the job search!

CodePudding user response:

  1. No need to switch to another window

    When you are opening the URL with that specific details in the for loop, the page is getting loaded one by one in the same window. Switch to window when there is a New window tab opened. Link to Refer

  2. Choose Explicit waits instead of time.sleep(). You have refined WebdriverWait but never used it.

  3. Try to come up with good locators. Go for Relative Xpath instead of Absolute Xpath.Link to Refer

Not sure what you are trying to do in try block. The locators does not highlight any elements in the page.

Refer below code:

# Imports
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome(service= Service("path to chromedriver.exe"))
driver.maximize_window()

driver.get("https://www.naukri.com")
wait = WebDriverWait(driver,30)

login_btn = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[text()='Login']")))
login_btn.click()

email = wait.until(EC.element_to_be_clickable((By.XPATH,"//form[@name='login-form']//input[contains(@placeholder,'Email')]")))
email.send_keys("[email protected]")

password = wait.until(EC.element_to_be_clickable((By.XPATH,"//form[@name='login-form']//input[contains(@placeholder,'password')]")))
password.send_keys("password")

password.submit()

role = "marketing-manager"
loc = "bangalore"
llimt = "15"
ulimit = "25"
for i in range(1,6):
    driver.get(f"https://www.naukri.com/{role}-jobs-in-{loc}-{i}?ctcFilter={llimt}to{ulimit}")
    time.sleep(2)
    # Update for Chat bot
    try:
        chatbot_close = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='chatbot_Nav']/div")))
        chatbot_close.click()
    except:
        print("Chat bot did not appear")
  • Related