forked from AIHawk-FOSS/Auto_Jobs_Applier_AI_Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedIn_authenticator.py
94 lines (84 loc) · 3.7 KB
/
linkedIn_authenticator.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
import time
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class LinkedInAuthenticator:
def __init__(self, driver=None):
self.driver = driver
self.email = ""
self.password = ""
def set_secrets(self, email, password):
self.email = email
self.password = password
def start(self):
"""Start the Chrome browser and attempt to log in to LinkedIn."""
print("Starting Chrome browser to log in to LinkedIn.")
self.driver.get('https://www.linkedin.com')
self.wait_for_page_load()
if not self.is_logged_in():
self.handle_login()
def handle_login(self):
"""Handle the LinkedIn login process."""
print("Navigating to the LinkedIn login page...")
self.driver.get("https://www.linkedin.com/login")
try:
self.enter_credentials()
self.submit_login_form()
except NoSuchElementException:
print("Could not log in to LinkedIn. Please check your credentials.")
time.sleep(35) #TODO fix better
self.handle_security_check()
def enter_credentials(self):
"""Enter the user's email and password into the login form."""
try:
email_field = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
email_field.send_keys(self.email)
password_field = self.driver.find_element(By.ID, "password")
password_field.send_keys(self.password)
except TimeoutException:
print("Login form not found. Aborting login.")
def submit_login_form(self):
"""Submit the LinkedIn login form."""
try:
login_button = self.driver.find_element(By.XPATH, '//button[@type="submit"]')
login_button.click()
except NoSuchElementException:
print("Login button not found. Please verify the page structure.")
def handle_security_check(self):
"""Handle LinkedIn security checks if triggered."""
try:
WebDriverWait(self.driver, 10).until(
EC.url_contains('https://www.linkedin.com/checkpoint/challengesV2/')
)
print("Security checkpoint detected. Please complete the challenge.")
WebDriverWait(self.driver, 300).until(
EC.url_contains('https://www.linkedin.com/feed/')
)
print("Security check completed")
except TimeoutException:
print("Security check not completed. Please try again later.")
def is_logged_in(self):
"""Check if the user is already logged in to LinkedIn."""
self.driver.get('https://www.linkedin.com/feed')
try:
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'share-box-feed-entry__trigger'))
)
buttons = self.driver.find_elements(By.CLASS_NAME, 'share-box-feed-entry__trigger')
if any(button.text.strip() == 'Start a post' for button in buttons):
print("User is already logged in.")
return True
except TimeoutException:
pass
return False
def wait_for_page_load(self, timeout=10):
"""Wait for the page to fully load."""
try:
WebDriverWait(self.driver, timeout).until(
lambda d: d.execute_script('return document.readyState') == 'complete'
)
except TimeoutException:
print("Page load timed out.")