linkedIn_auto_jobs_applier_with_AI
87 строк · 3.3 Кб
1import time
2from selenium.common.exceptions import NoSuchElementException, TimeoutException
3from selenium.webdriver.common.by import By
4from selenium.webdriver.support.ui import WebDriverWait
5from selenium.webdriver.support import expected_conditions as EC
6
7class LinkedInAuthenticator:
8
9def __init__(self, driver=None):
10self.driver = driver
11self.email = ""
12self.password = ""
13
14def set_secrets(self, email, password):
15self.email = email
16self.password = password
17
18def start(self):
19print("Starting Chrome browser to log in to LinkedIn.")
20self.driver.get('https://www.linkedin.com')
21self.wait_for_page_load()
22if not self.is_logged_in():
23self.handle_login()
24
25def handle_login(self):
26print("Navigating to the LinkedIn login page...")
27self.driver.get("https://www.linkedin.com/login")
28try:
29self.enter_credentials()
30self.submit_login_form()
31except NoSuchElementException:
32print("Could not log in to LinkedIn. Please check your credentials.")
33time.sleep(35) #TODO fix better
34self.handle_security_check()
35
36def enter_credentials(self):
37try:
38email_field = WebDriverWait(self.driver, 10).until(
39EC.presence_of_element_located((By.ID, "username"))
40)
41email_field.send_keys(self.email)
42password_field = self.driver.find_element(By.ID, "password")
43password_field.send_keys(self.password)
44except TimeoutException:
45print("Login form not found. Aborting login.")
46
47def submit_login_form(self):
48try:
49login_button = self.driver.find_element(By.XPATH, '//button[@type="submit"]')
50login_button.click()
51except NoSuchElementException:
52print("Login button not found. Please verify the page structure.")
53
54def handle_security_check(self):
55try:
56WebDriverWait(self.driver, 10).until(
57EC.url_contains('https://www.linkedin.com/checkpoint/challengesV2/')
58)
59print("Security checkpoint detected. Please complete the challenge.")
60WebDriverWait(self.driver, 300).until(
61EC.url_contains('https://www.linkedin.com/feed/')
62)
63print("Security check completed")
64except TimeoutException:
65print("Security check not completed. Please try again later.")
66
67def is_logged_in(self):
68self.driver.get('https://www.linkedin.com/feed')
69try:
70WebDriverWait(self.driver, 10).until(
71EC.presence_of_element_located((By.CLASS_NAME, 'share-box-feed-entry__trigger'))
72)
73buttons = self.driver.find_elements(By.CLASS_NAME, 'share-box-feed-entry__trigger')
74if any(button.text.strip() == 'Start a post' for button in buttons):
75print("User is already logged in.")
76return True
77except TimeoutException:
78pass
79return False
80
81def wait_for_page_load(self, timeout=10):
82try:
83WebDriverWait(self.driver, timeout).until(
84lambda d: d.execute_script('return document.readyState') == 'complete'
85)
86except TimeoutException:
87print("Page load timed out.")
88