TIL Python Basics Day 49 - Automating Job Applications on LinkedIn

이다연·2021년 2월 1일
0

Udemy Python Course

목록 보기
45/64

Full time studying in January 2021! 💯

Project: Automating Job Applications on LinkedIn

Goal

to automate job application on linkedin using selenium

Learning

This project was a complete 삽질; however, I managed to complete it.

maximize_window

selenium cannot locate if element is not visible on screen.

driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.maximize_window()

find element's' with for loop

next_co = driver.find_elements_by_css_selector("section.jobs-search__left-rail ul li a.job-card-list__title")
for company in next_co:
	pass

try & except

to use 'except error sign', I need to import the specific exeption class

time

Selenium needs to wait until the browser load the page. Otherwise, it causes an error.

import time
time.sleep(5)

Locating the element

copy and paste the path using right click doesn't work this situation.

locating the element to skip

get_attribute()

#If the submit_button is a "Next" button, then this is a multi-step application, so skip.
        if submit_button.get_attribute("data-control-name") == "continue_unify":
            close_button = driver.find_element_by_class_name("artdeco-modal__dismiss")
            close_button.click()

exception handling

to use exception handling, we need to import it

from selenium.common.exceptions import NoSuchElementException

Problem to solve

There are total 17 jobs in the list. Selenium only fetches 7 jobs that is visible on the screen. How can I solve this problem?
1. scroll down?
2. screen size 50% down so it shows everythng on the screen

Final code

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

chrome_driver_path = "C:\dayeon2020\chromedriver.exe" #for mac: no .exe
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.maximize_window()

driver.get("https://www.linkedin.com/jobs/search/?f_LF=f_AL&geoId=105149562&"
           "keywords=Python%20developer&location=%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD")
time.sleep(4)

login_btn = driver.find_element_by_xpath("/html/body/div[3]/a[1]")
login_btn.click()

username = driver.find_element_by_id("username")
username.send_keys("testpythondy@gmail.com")
password = driver.find_element_by_id("password")
password.send_keys("-")

login_click_btn = driver.find_element_by_xpath("//*[@id='app__container']/main/div[2]/form/div[3]")
login_click_btn.click()
time.sleep(2)

#-------------------------------------job apply--------------------------------

next_co = driver.find_elements_by_css_selector("section.jobs-search__left-rail ul li a.job-card-list__title")
for company in next_co:
    # co = driver.find_elements_by_css_selector("li .a job-card-list__title")
    company.click()
    time.sleep(5)

    driver.find_element_by_css_selector(".jobs-apply-button--top-card button").click()  # easy_apply
    time.sleep(2)

    #driver.find_element_by_css_selector(".display-flex button")
    try:
        send_btn = driver.find_element_by_css_selector(".display-flex button.artdeco-button--primary")
        # send_btn.click()
        # time.sleep(3)

        x_btn = driver.find_element_by_class_name("mercado-match")
        x_btn.click()
        time.sleep(3)

        xx_btn = driver.find_element_by_css_selector(".artdeco-modal__actionbar button.artdeco-button--primary")
        xx_btn.click()
        time.sleep(3)
        print(f"{company.text} applied")
        print("-------------------------")

    except NoSuchElementException:
        pass

driver.quit()

Result

Angela's code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time

ACCOUNT_EMAIL = YOUR LOGIN EMAIL
ACCOUNT_PASSWORD = YOUR LOGIN PASSWORD
PHONE = YOUR PHONE NUMBER

chrome_driver_path = YOUR CHROME DRIVER PATH
driver = webdriver.Chrome(chrome_driver_path)
driver.get("https://www.linkedin.com/jobs/search/?f_LF=f_AL&geoId=102257491&keywords=marketing%20intern&location=London%2C%20England%2C%20United%20Kingdom&redirect=false&position=1&pageNum=0")

time.sleep(2)
sign_in_button = driver.find_element_by_link_text("Sign in")
sign_in_button.click()

time.sleep(5)
email_field = driver.find_element_by_id("username")
email_field.send_keys(ACCOUNT_EMAIL)
password_field = driver.find_element_by_id("password")
password_field.send_keys(ACCOUNT_PASSWORD)
password_field.send_keys(Keys.ENTER)

time.sleep(5)

all_listings = driver.find_elements_by_css_selector(".job-card-container--clickable")

for listing in all_listings:
    print("called")
    listing.click()
    time.sleep(2)

    #Try to locate the apply button, if can't locate then skip the job.
    try:
        apply_button = driver.find_element_by_css_selector(".jobs-s-apply button")
        apply_button.click()
        time.sleep(5)
        
        #If phone field is empty, then fill your phone number.
        phone = driver.find_element_by_class_name("fb-single-line-text__input")
        if phone.text == "":
            phone.send_keys(PHONE)

        submit_button = driver.find_element_by_css_selector("footer button")

        #If the submit_button is a "Next" button, then this is a multi-step application, so skip.
        if submit_button.get_attribute("data-control-name") == "continue_unify":
            close_button = driver.find_element_by_class_name("artdeco-modal__dismiss")
            close_button.click()
            time.sleep(2)
            discard_button = driver.find_elements_by_class_name("artdeco-modal__confirm-dialog-btn")[1]
            discard_button.click()
            print("Complex application, skipped.")
            continue
        else:
            submit_button.click()
    
        #Once application completed, close the pop-up window.
        time.sleep(2)
        close_button = driver.find_element_by_class_name("artdeco-modal__dismiss")
        close_button.click()

    #If already applied to job or job is no longer accepting applications, then skip.
    except NoSuchElementException:
        print("No application button, skipped.")
        continue

time.sleep(5)
driver.quit()
profile
Dayeon Lee | Django & Python Web Developer

0개의 댓글