Skip to content

Add LinkedIn automation script to withdraw sent invitations #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions linkedin_withdrew_request/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 💼 LinkedIn Invitation Cleanup Script

A robust, Selenium-based automation script to withdraw all pending LinkedIn invitations. Designed with explicit waits, modular structure, and comprehensive error handling to ensure stability and reliability across Chrome and Edge browsers.

---

## ⚙️ Features

- ✅ Automates withdrawal of sent LinkedIn invitations
- ✅ Explicit wait handling to avoid flakiness
- ✅ Cross-browser compatibility (Chrome & Edge)
- ✅ Modular functions for easy maintenance
- ✅ Graceful error handling with clear logs

---

## 🧰 Requirements

- Python 3.7+
- Google Chrome or Microsoft Edge
- ChromeDriver / EdgeDriver installed and added to PATH

### Python Dependencies
Install via pip:

```bash
pip install -r requirements.txt

🛡️ Best Practices
- 💡 Use explicit waits WebDriverWait instead of hard sleeps for robustness.
- 🚫 Avoid hardcoded XPaths; use stable selectors when possible.
- 🧪 Test regularly as LinkedIn’s DOM may change.
- 🔄 Modularize helpers to reuse and scale your automation.

🤝 Contributing
PRs are welcome! Please follow standard formatting conventions and include docstrings. For major changes, open an issue first to discuss what you’d like to change.

📄 License
This project is licensed under the MIT License. See LICENSE for details.

---
74 changes: 74 additions & 0 deletions linkedin_withdrew_request/delete_linkedin_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import (
StaleElementReferenceException,
NoSuchElementException,
TimeoutException,
)
import time


def withdraw_invitations():
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 15)
driver.maximize_window()

try:
# Step 1: Manual Login
driver.get("https://www.linkedin.com/login")
print("⏳ Please log in manually within 20 seconds...")
time.sleep(20)

# Step 2: Navigate to Sent Invitations
driver.get("https://www.linkedin.com/mynetwork/invitation-manager/sent/")
wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)

# Step 3: Loop for Withdrawals
while True:
try:
wait.until(EC.presence_of_all_elements_located(
(By.XPATH, "//span[contains(text(), 'Withdraw')]")))
withdraw_buttons = driver.find_elements(By.XPATH, "//span[contains(text(), 'Withdraw')]")

if not withdraw_buttons:
print("✅ All invitations withdrawn or none left.")
break

for button in withdraw_buttons:
try:
driver.execute_script("arguments[0].click();", button)
confirm_button = wait.until(EC.presence_of_element_located(
(By.XPATH,
"//h2[contains(text(), 'Withdraw invitation')]/ancestor::header/../div//button[contains(@aria-label,'Withdraw')]")
))
driver.execute_script("arguments[0].click();", confirm_button)
time.sleep(2)
except (StaleElementReferenceException, NoSuchElementException, TimeoutException) as e:
print("⚠️ Skipped one due to:", e)
continue

# Load more invitations
try:
load_more = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//span[text()='Load more']")
))
driver.execute_script("arguments[0].scrollIntoView();", load_more)
load_more.click()
time.sleep(3)
except TimeoutException:
print("📦 No more invitations to load.")
break

except TimeoutException:
print("⏰ Timeout while finding 'Withdraw' buttons.")
break
finally:
driver.quit()
print("🚪 Done and browser closed.")


# Run it
withdraw_invitations()
1 change: 1 addition & 0 deletions linkedin_withdrew_request/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
selenium>=4.0.0
Loading