forked from nicolomantini/LinkedIn-Easy-Apply-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyapplybot.py
398 lines (330 loc) · 14 KB
/
easyapplybot.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import time
import random
import os
import csv
import datetime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import pandas as pd
import pyautogui
from tkinter import filedialog, Tk
import tkinter.messagebox as tm
from urllib.request import urlopen
import loginGUI
# pyinstaller --onefile --windowed --icon=app.ico easyapplybot.py
class EasyApplyBot:
MAX_APPLICATIONS = 500
def __init__(self, username, password, language, position, location, resumeloctn, appliedJobIDs, filename):
print("\nWelcome to Easy Apply Bot\n")
dirpath = os.getcwd()
print("current directory is : " + dirpath)
chromepath = dirpath + '/assets/chromedriver.exe'
#foldername = os.path.basename(dirpath)
#print("Directory name is : " + foldername)
self.language = language
self.appliedJobIDs = appliedJobIDs
self.filename = filename
self.options = self.browser_options()
#self.browser = webdriver.Chrome()
#self.browser = webdriver.Chrome(executable_path = "C:/chromedriver_win32/chromedriver.exe")
self.browser = webdriver.Chrome(
chrome_options=self.options, executable_path=chromepath)
self.wait = WebDriverWait(self.browser, 30)
self.start_linkedin(username, password)
def browser_options(self):
options = Options()
options.add_argument("--start-maximized")
options.add_argument("--ignore-certificate-errors")
#options.add_argument("user-agent=Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393")
# options.add_argument('--headless')
options.add_argument('--no-sandbox')
# options.add_argument('--disable-gpu')
# options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
return options
def start_linkedin(self, username, password):
print("\nLogging in.....\n \nPlease wait :) \n ")
self.browser.get(
"https://www.linkedin.com/login?trk=guest_homepage-basic_nav-header-signin")
try:
user_field = self.browser.find_element_by_id("username")
pw_field = self.browser.find_element_by_id("password")
login_button = self.browser.find_element_by_css_selector(
".btn__primary--large")
user_field.send_keys(username)
user_field.send_keys(Keys.TAB)
time.sleep(1)
pw_field.send_keys(password)
time.sleep(1)
login_button.click()
except TimeoutException:
print("TimeoutException! Username/password field or login button not found")
def wait_for_login(self):
if language == "en":
title = "Sign In to LinkedIn"
elif language == "es":
title = "Inicia sesión"
elif language == "pt":
title = "Entrar no LinkedIn"
time.sleep(1)
while True:
if self.browser.title != title:
print("\nStarting LinkedIn bot\n")
break
else:
time.sleep(1)
print("\nPlease Login to your LinkedIn account\n")
def fill_data(self):
self.browser.set_window_size(0, 0)
self.browser.set_window_position(2000, 2000)
os.system("reset")
self.position = position
self.location = "&location=" + location
self.resumeloctn = resumeloctn
print(self.resumeloctn)
def start_apply(self):
# self.wait_for_login()
self.fill_data()
self.applications_loop()
def applications_loop(self):
count_application = 0
count_job = 0
jobs_per_page = 0
os.system("reset")
print("\nLooking for jobs.. Please wait..\n")
self.browser.set_window_position(0, 0)
self.browser.maximize_window()
self.browser, _ = self.next_jobs_page(jobs_per_page)
print("\nLooking for jobs.. Please wait..\n")
# below was causing issues, and not sure what they are for.
# self.browser.find_element_by_class_name("jobs-search-dropdown__trigger-icon").click()
# self.browser.find_element_by_class_name("jobs-search-dropdown__option").click()
#self.job_page = self.load_page(sleep=0.5)
while count_application < self.MAX_APPLICATIONS:
# sleep to make sure everything loads, add random to make us look human.
time.sleep(random.uniform(3.5, 6.9))
self.load_page(sleep=1)
# get job links
links = self.browser.find_elements_by_xpath(
'//div[@data-job-id]'
)
# get job ID of each job link
IDs = []
for link in links:
temp = link.get_attribute("data-job-id")
jobID = temp.split(":")[-1]
IDs.append(int(jobID))
IDs = set(IDs)
# remove already applied jobs
jobIDs = [x for x in IDs if x not in self.appliedJobIDs]
if len(jobIDs) == 0:
jobs_per_page = jobs_per_page + 25
count_job = 0
self.avoid_lock()
self.browser, jobs_per_page = self.next_jobs_page(
jobs_per_page)
# loop over IDs to apply
for jobID in jobIDs:
count_job += 1
self.get_job_page(jobID)
# get easy apply button
button = self.get_easy_apply_button()
if button is not False:
string_easy = "* has Easy Apply Button"
button.click()
self.send_resume()
count_application += 1
else:
string_easy = "* Doesn't have Easy Apply Button"
position_number = str(count_job + jobs_per_page)
print(
f"\nPosition {position_number}:\n {self.browser.title} \n {string_easy} \n")
# append applied job ID to csv file
timestamp = datetime.datetime.now()
toWrite = [timestamp, jobID]
with open(self.filename, 'a') as f:
writer = csv.writer(f)
writer.writerow(toWrite)
# sleep every 20 applications
if count_application % 20 == 0:
sleepTime = random.randint(50, 90)
print('\n\n****************************************\n\n')
#print('Time for a nap - see you in: ' + int(sleepTime/60) + 'min..')
print('\n\n****************************************\n\n')
time.sleep(sleepTime)
# go to new page if all jobs are done
if count_job == len(jobIDs):
jobs_per_page = jobs_per_page + 25
count_job = 0
print('\n\n****************************************\n\n')
print('Going to next jobs page, YEAAAHHH!!')
print('\n\n****************************************\n\n')
self.avoid_lock()
self.browser, jobs_per_page = self.next_jobs_page(
jobs_per_page)
self.finish_apply()
def get_job_links(self, page):
links = []
for link in page.find_all('a'):
url = link.get('href')
if url:
if '/jobs/view' in url:
links.append(url)
return set(links)
def get_job_page(self, jobID):
#root = 'www.linkedin.com'
# if root not in job:
job = 'https://www.linkedin.com/jobs/view/' + str(jobID)
self.browser.get(job)
self.job_page = self.load_page(sleep=0.5)
return self.job_page
def got_easy_apply(self, page):
#button = page.find("button", class_="jobs-apply-button artdeco-button jobs-apply-button--top-card artdeco-button--3 ember-view")
button = self.browser.find_elements_by_xpath(
'//button[contains(@class, "jobs-apply")]/span[1]'
)
EasyApplyButton = button[0]
if EasyApplyButton.text in "Easy Apply":
return EasyApplyButton
else:
return False
# return len(str(button)) > 4
def get_easy_apply_button(self):
try:
button = self.browser.find_elements_by_xpath(
'//button[contains(@class, "jobs-apply")]/span[1]'
)
# if button[0].text in "Easy Apply" :
EasyApplyButton = button[0]
except:
EasyApplyButton = False
return EasyApplyButton
def easy_apply_xpath(self):
button = self.get_easy_apply_button()
button_inner_html = str(button)
list_of_words = button_inner_html.split()
next_word = [
word for word in list_of_words if "ember" in word and "id" in word]
ember = next_word[0][:-1]
xpath = '//*[@'+ember+']/button'
return xpath
def click_button(self, xpath):
triggerDropDown = self.browser.find_element_by_xpath(xpath)
time.sleep(0.5)
triggerDropDown.click()
time.sleep(1)
def send_resume(self):
try:
# self.browser.find_element_by_xpath('//*[@id="file-browse-input"]').send_keys(self.resumeloctn)
submit_button = self.wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button[aria-label='Submit application']")))
"""
time.sleep(3)
while not submit_button:
if language == "en":
submit_button = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Submit application']")))
elif language == "es":
submit_button = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Enviar solicitud']")))
#submit_button = self.browser.find_element_by_xpath("//*[contains(text(), 'Enviar solicitud')]")
elif language == "pt":
submit_button = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Enviar candidatura']")))
#submit_button = self.browser.find_element_by_xpath("//*[contains(text(), 'Enviar candidatura')]")
"""
submit_button.click()
time.sleep(10)
# After submiting the application, a dialog shows up, we need to close this dialog
close_button = self.wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button[aria-label='Dismiss']")))
close_button.click()
time.sleep(10)
except:
print("cannot apply to this job")
def load_page(self, sleep=1):
scroll_page = 0
while scroll_page < 4000:
self.browser.execute_script(
"window.scrollTo(0,"+str(scroll_page)+" );")
scroll_page += 200
time.sleep(sleep)
if sleep != 1:
self.browser.execute_script("window.scrollTo(0,0);")
time.sleep(sleep * 3)
page = BeautifulSoup(self.browser.page_source, "lxml")
return page
def avoid_lock(self):
#x, _ = pyautogui.position()
#pyautogui.moveTo(x+200, None, duration=1.0)
#pyautogui.moveTo(x, None, duration=0.5)
# pyautogui.keyDown('ctrl')
# pyautogui.press('esc')
# pyautogui.keyUp('ctrl')
# time.sleep(0.5)
# pyautogui.press('esc')
print('Avoiding Lock')
def next_jobs_page(self, jobs_per_page):
self.browser.get(
"https://www.linkedin.com/jobs/search/?f_LF=f_AL&keywords=" +
self.position + self.location + "&start="+str(jobs_per_page))
self.avoid_lock()
self.load_page()
return (self.browser, jobs_per_page)
def finish_apply(self):
self.browser.close()
if __name__ == '__main__':
# set use of gui (T/F)
#useGUI = True
useGUI = False
# use gui
if useGUI == True:
app = loginGUI.LoginGUI()
app.mainloop()
# get user info info
username = app.frames["StartPage"].username
password = app.frames["StartPage"].password
language = app.frames["PageOne"].language
position = app.frames["PageTwo"].position
location_code = app.frames["PageThree"].location_code
if location_code == 1:
location = app.frames["PageThree"].location
else:
location = app.frames["PageFour"].location
resumeloctn = app.frames["PageFive"].resumeloctn
# no gui
if useGUI == False:
username = '********@****.com' # LinkedIn Username
password = '********' # LinkedIn Password
language = 'en' # LinkedIn default language (en, es, pt)
position = 'C# Developer' # Job position
location = 'South Africa' # location where to apply (e.g. Barcelona)
#dir = os.getcwd()
resumepath = 'assets/pdf/resume.pdf'
#print("resume directory is : " + resumepath)
resumeloctn = resumepath # Directory path where your document is
# print input
print("\nThese is your input:")
print(
"\nUsername: " + username,
"\nPassword: " + password,
"\nLanguage: " + language,
"\nPosition: " + position,
"\nLocation: " + location
)
print("\nLet's scrape some jobs!\n")
# get list of already applied jobs
filename = 'joblist.csv'
try:
df = pd.read_csv(filename, header=None)
appliedJobIDs = list(df.iloc[:, 1])
except:
appliedJobIDs = []
# start bot
bot = EasyApplyBot(username, password, language, position,
location, resumeloctn, appliedJobIDs, filename)
bot.start_apply()