Loguru Integration: Better logs

This commit is contained in:
tapas-joshi 2024-09-12 21:30:35 -04:00
parent c27bf2c715
commit f77706d579
14 changed files with 851 additions and 374 deletions

View file

@ -6,7 +6,7 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from src.utils import logger
from loguru import logger
class LinkedInAuthenticator:
@ -15,12 +15,12 @@ class LinkedInAuthenticator:
self.driver = driver
self.email = ""
self.password = ""
logger.debug("LinkedInAuthenticator initialized with driver: %s", driver)
logger.debug(f"LinkedInAuthenticator initialized with driver: {driver}")
def set_secrets(self, email, password):
self.email = email
self.password = password
logger.debug("Secrets set with email: %s", email)
logger.debug(f"Secrets set with email: {email}")
def start(self):
logger.info("Starting Chrome browser to log in to LinkedIn.")
@ -40,13 +40,13 @@ class LinkedInAuthenticator:
logger.info("Navigating to the LinkedIn login page...")
self.driver.get("https://www.linkedin.com/login")
if 'feed' in self.driver.current_url:
print("User is already logged in.")
logger.debug("User is already logged in.")
return
try:
self.enter_credentials()
self.submit_login_form()
except NoSuchElementException as e:
logger.error("Could not log in to LinkedIn. Element not found: %s", e)
logger.error(f"Could not log in to LinkedIn. Element not found: {e}")
time.sleep(random.uniform(3, 5))
self.handle_security_check()
@ -57,13 +57,13 @@ class LinkedInAuthenticator:
EC.presence_of_element_located((By.ID, "username"))
)
email_field.send_keys(self.email)
logger.debug("Email entered: %s", self.email)
logger.debug(f"Email entered: {self.email}")
password_field = self.driver.find_element(By.ID, "password")
password_field.send_keys(self.password)
logger.debug("Password entered.")
except TimeoutException:
logger.error("Login form not found. Aborting login.")
print("Login form not found. Aborting login.")
logger.error("Login form not found. Aborting login.")
def submit_login_form(self):
try:
@ -73,7 +73,6 @@ class LinkedInAuthenticator:
logger.debug("Login form submitted.")
except NoSuchElementException:
logger.error("Login button not found. Please verify the page structure.")
print("Login button not found. Please verify the page structure.")
def handle_security_check(self):
try:
@ -82,22 +81,19 @@ class LinkedInAuthenticator:
EC.url_contains('https://www.linkedin.com/checkpoint/challengesV2/')
)
logger.warning("Security checkpoint detected. Please complete the challenge.")
print("Security checkpoint detected. Please complete the challenge.")
WebDriverWait(self.driver, 300).until(
EC.url_contains('https://www.linkedin.com/feed/')
)
logger.info("Security check completed")
print("Security check completed")
except TimeoutException:
logger.error("Security check not completed within the timeout.")
print("Security check not completed. Please try again later.")
logger.error("Security check not completed. Please try again later.")
def is_logged_in(self):
# target_url = 'https://www.linkedin.com/feed'
#
# # Navigate to the target URL if not already there
# if self.driver.current_url != target_url:
# logger.debug("Navigating to target URL: %s", target_url)
# logger.debug(f"Navigating to target URL: {target_url}")
# self.driver.get(target_url)
try:
@ -109,10 +105,10 @@ class LinkedInAuthenticator:
# Check for the presence of the "Start a post" button
buttons = self.driver.find_elements(By.CLASS_NAME, 'share-box-feed-entry__trigger')
logger.debug("Found %d 'Start a post' buttons", len(buttons))
logger.debug(f"Found {len(buttons)} 'Start a post' buttons")
for i, button in enumerate(buttons):
logger.debug("Button %d text: %s", i + 1, button.text.strip())
logger.debug(f"Button {i + 1} text: {button.text.strip()}")
if any(button.text.strip().lower() == 'start a post' for button in buttons):
logger.info("Found 'Start a post' button indicating user is logged in.")
@ -132,11 +128,10 @@ class LinkedInAuthenticator:
def wait_for_page_load(self, timeout=10):
try:
logger.debug("Waiting for page to load with timeout: %s seconds", timeout)
logger.debug(f"Waiting for page to load with timeout: {timeout} seconds")
WebDriverWait(self.driver, timeout).until(
lambda d: d.execute_script('return document.readyState') == 'complete'
)
logger.debug("Page load completed.")
except TimeoutException:
logger.error("Page load timed out.")
print("Page load timed out.")