linkedIn_auto_jobs_applier_with_AI

Форк
0
/
linkedIn_authenticator.py 
87 строк · 3.3 Кб
1
import time
2
from selenium.common.exceptions import NoSuchElementException, TimeoutException
3
from selenium.webdriver.common.by import By
4
from selenium.webdriver.support.ui import WebDriverWait
5
from selenium.webdriver.support import expected_conditions as EC
6

7
class LinkedInAuthenticator:
8
    
9
    def __init__(self, driver=None):
10
        self.driver = driver
11
        self.email = ""
12
        self.password = ""
13

14
    def set_secrets(self, email, password):
15
        self.email = email
16
        self.password = password
17

18
    def start(self):
19
        print("Starting Chrome browser to log in to LinkedIn.")
20
        self.driver.get('https://www.linkedin.com')
21
        self.wait_for_page_load()
22
        if not self.is_logged_in():
23
            self.handle_login()
24

25
    def handle_login(self):
26
        print("Navigating to the LinkedIn login page...")
27
        self.driver.get("https://www.linkedin.com/login")
28
        try:
29
            self.enter_credentials()
30
            self.submit_login_form()
31
        except NoSuchElementException:
32
            print("Could not log in to LinkedIn. Please check your credentials.")
33
        time.sleep(35) #TODO fix better
34
        self.handle_security_check()
35

36
    def enter_credentials(self):
37
        try:
38
            email_field = WebDriverWait(self.driver, 10).until(
39
                EC.presence_of_element_located((By.ID, "username"))
40
            )
41
            email_field.send_keys(self.email)
42
            password_field = self.driver.find_element(By.ID, "password")
43
            password_field.send_keys(self.password)
44
        except TimeoutException:
45
            print("Login form not found. Aborting login.")
46

47
    def submit_login_form(self):
48
        try:
49
            login_button = self.driver.find_element(By.XPATH, '//button[@type="submit"]')
50
            login_button.click()
51
        except NoSuchElementException:
52
            print("Login button not found. Please verify the page structure.")
53

54
    def handle_security_check(self):
55
        try:
56
            WebDriverWait(self.driver, 10).until(
57
                EC.url_contains('https://www.linkedin.com/checkpoint/challengesV2/')
58
            )
59
            print("Security checkpoint detected. Please complete the challenge.")
60
            WebDriverWait(self.driver, 300).until(
61
                EC.url_contains('https://www.linkedin.com/feed/')
62
            )
63
            print("Security check completed")
64
        except TimeoutException:
65
            print("Security check not completed. Please try again later.")
66

67
    def is_logged_in(self):
68
        self.driver.get('https://www.linkedin.com/feed')
69
        try:
70
            WebDriverWait(self.driver, 10).until(
71
                EC.presence_of_element_located((By.CLASS_NAME, 'share-box-feed-entry__trigger'))
72
            )
73
            buttons = self.driver.find_elements(By.CLASS_NAME, 'share-box-feed-entry__trigger')
74
            if any(button.text.strip() == 'Start a post' for button in buttons):
75
                print("User is already logged in.")
76
                return True
77
        except TimeoutException:
78
            pass
79
        return False
80

81
    def wait_for_page_load(self, timeout=10):
82
        try:
83
            WebDriverWait(self.driver, timeout).until(
84
                lambda d: d.execute_script('return document.readyState') == 'complete'
85
            )
86
        except TimeoutException:
87
            print("Page load timed out.")
88

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.