Job Applying Bot in Python facing IndexError: list index out of range
Job Applying Bot in Python facing IndexError: list index out of range
免责声明:我将在十多年后重新开始编写脚本(一开始我还是个新手),如果问题很普通但非常需要帮助,我们深表歉意。
我正在尝试修改 python 脚本以登录到职位门户,根据属性搜索职位空缺,然后申请上述职位。
由于门户网站在单独的选项卡上打开新的职位空缺,代码应该转到下一个选项卡并测试职位描述中的标准
代码片段如下:
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])
但是,当我 运行 脚本时,它只是让我登录到门户并转到正确的列表页面,但只是停留在那里。另外,它会弹出此错误:
> line 43, in <module> driver.switch_to.window(driver.window_handles[1])
> IndexError: list index out of range
无法理解这是指哪个列表以及如何修复此代码。感谢任何帮助。
感兴趣者的完整代码:
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])
提前感谢您的回答!对求职很有帮助!
无需切换到另一个window
当您打开带有 for loop
中特定详细信息的 URL
时,页面将在相同的 window 中一个接一个地加载。打开新 window 选项卡时切换到 window。 Link to Refer
选择显式等待而不是 time.sleep()
。您精炼了 WebdriverWait 但从未使用过它。
试着想出好的定位器。选择相对 Xpath 而不是绝对 Xpath。Link to Refer
不确定您要在 try
块中做什么。定位器不会突出显示页面中的任何元素。
参考以下代码:
# 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("abc@gmail.com")
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")
免责声明:我将在十多年后重新开始编写脚本(一开始我还是个新手),如果问题很普通但非常需要帮助,我们深表歉意。
我正在尝试修改 python 脚本以登录到职位门户,根据属性搜索职位空缺,然后申请上述职位。
由于门户网站在单独的选项卡上打开新的职位空缺,代码应该转到下一个选项卡并测试职位描述中的标准
代码片段如下:
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])
但是,当我 运行 脚本时,它只是让我登录到门户并转到正确的列表页面,但只是停留在那里。另外,它会弹出此错误:
> line 43, in <module> driver.switch_to.window(driver.window_handles[1])
> IndexError: list index out of range
无法理解这是指哪个列表以及如何修复此代码。感谢任何帮助。
感兴趣者的完整代码:
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])
提前感谢您的回答!对求职很有帮助!
无需切换到另一个window
当您打开带有
for loop
中特定详细信息的URL
时,页面将在相同的 window 中一个接一个地加载。打开新 window 选项卡时切换到 window。 Link to Refer选择显式等待而不是
time.sleep()
。您精炼了 WebdriverWait 但从未使用过它。试着想出好的定位器。选择相对 Xpath 而不是绝对 Xpath。Link to Refer
不确定您要在 try
块中做什么。定位器不会突出显示页面中的任何元素。
参考以下代码:
# 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("abc@gmail.com")
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")