Skip to content

Commit b3b1165

Browse files
authored
Merge pull request #1022 from ayush-a-srivastava/linkedin_withdrew_request
Add LinkedIn automation script to withdraw sent invitations
2 parents 4fa945c + 46823ff commit b3b1165

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

linkedin_withdrew_request/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 💼 LinkedIn Invitation Cleanup Script
2+
3+
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.
4+
5+
---
6+
7+
## ⚙️ Features
8+
9+
- ✅ Automates withdrawal of sent LinkedIn invitations
10+
- ✅ Explicit wait handling to avoid flakiness
11+
- ✅ Cross-browser compatibility (Chrome & Edge)
12+
- ✅ Modular functions for easy maintenance
13+
- ✅ Graceful error handling with clear logs
14+
15+
---
16+
17+
## 🧰 Requirements
18+
19+
- Python 3.7+
20+
- Google Chrome or Microsoft Edge
21+
- ChromeDriver / EdgeDriver installed and added to PATH
22+
23+
### Python Dependencies
24+
Install via pip:
25+
26+
```bash
27+
pip install -r requirements.txt
28+
29+
🛡️ Best Practices
30+
- 💡 Use explicit waits WebDriverWait instead of hard sleeps for robustness.
31+
- 🚫 Avoid hardcoded XPaths; use stable selectors when possible.
32+
- 🧪 Test regularly as LinkedIn’s DOM may change.
33+
- 🔄 Modularize helpers to reuse and scale your automation.
34+
35+
🤝 Contributing
36+
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.
37+
38+
📄 License
39+
This project is licensed under the MIT License. See LICENSE for details.
40+
41+
---
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from selenium import webdriver
2+
from selenium.webdriver.common.by import By
3+
from selenium.webdriver.support.ui import WebDriverWait
4+
from selenium.webdriver.support import expected_conditions as EC
5+
from selenium.common.exceptions import (
6+
StaleElementReferenceException,
7+
NoSuchElementException,
8+
TimeoutException,
9+
)
10+
import time
11+
12+
13+
def withdraw_invitations():
14+
driver = webdriver.Chrome()
15+
wait = WebDriverWait(driver, 15)
16+
driver.maximize_window()
17+
18+
try:
19+
# Step 1: Manual Login
20+
driver.get("https://www.linkedin.com/login")
21+
print("⏳ Please log in manually within 20 seconds...")
22+
time.sleep(20)
23+
24+
# Step 2: Navigate to Sent Invitations
25+
driver.get("https://www.linkedin.com/mynetwork/invitation-manager/sent/")
26+
wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
27+
time.sleep(2)
28+
29+
# Step 3: Loop for Withdrawals
30+
while True:
31+
try:
32+
wait.until(EC.presence_of_all_elements_located(
33+
(By.XPATH, "//span[contains(text(), 'Withdraw')]")))
34+
withdraw_buttons = driver.find_elements(By.XPATH, "//span[contains(text(), 'Withdraw')]")
35+
36+
if not withdraw_buttons:
37+
print("✅ All invitations withdrawn or none left.")
38+
break
39+
40+
for button in withdraw_buttons:
41+
try:
42+
driver.execute_script("arguments[0].click();", button)
43+
confirm_button = wait.until(EC.presence_of_element_located(
44+
(By.XPATH,
45+
"//h2[contains(text(), 'Withdraw invitation')]/ancestor::header/../div//button[contains(@aria-label,'Withdraw')]")
46+
))
47+
driver.execute_script("arguments[0].click();", confirm_button)
48+
time.sleep(2)
49+
except (StaleElementReferenceException, NoSuchElementException, TimeoutException) as e:
50+
print("⚠️ Skipped one due to:", e)
51+
continue
52+
53+
# Load more invitations
54+
try:
55+
load_more = wait.until(EC.element_to_be_clickable(
56+
(By.XPATH, "//span[text()='Load more']")
57+
))
58+
driver.execute_script("arguments[0].scrollIntoView();", load_more)
59+
load_more.click()
60+
time.sleep(3)
61+
except TimeoutException:
62+
print("📦 No more invitations to load.")
63+
break
64+
65+
except TimeoutException:
66+
print("⏰ Timeout while finding 'Withdraw' buttons.")
67+
break
68+
finally:
69+
driver.quit()
70+
print("🚪 Done and browser closed.")
71+
72+
73+
# Run it
74+
withdraw_invitations()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
selenium>=4.0.0

0 commit comments

Comments
 (0)