Last Updated : 12 Jul, 2025
Automating LinkedIn connections using Python involves creating a script that navigates LinkedIn, finds users based on specific criteria (e.g., job title, company, or location), and sends personalized connection requests. In this article, we will walk you through the process, using Selenium for web automation. This automation simplifies networking while maintaining a personal touch.
Step 1: Install Required LibrariesWe need a few libraries to perform web automation and handle web elements effectively.
pip install selenium webdriver-managerStep 2: Set Up the Environment
To automate LinkedIn connections, you’ll need:
Let’s begin by importing the required libraries into your Python script.
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
Step 4: Log in to LinkedIn
We will automate the login process to access LinkedIn.
# Set up the browser
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.linkedin.com/login")
# Enter login credentials
username = driver.find_element(By.ID, "username")
password = driver.find_element(By.ID, "password")
username.send_keys("your_email@example.com")
password.send_keys("your_password")
# Click login button
login_button = driver.find_element(By.XPATH, "//button[@type='submit']")
login_button.click()
time.sleep(3) # Wait for the page to load
Step 5: Search for Profiles
Search for users based on specific criteria like job titles or companies.
search_box = driver.find_element(By.CSS_SELECTOR, ".search-global-typeahead__input.search-global-typeahead__input--ellipsis")
search_box.send_keys("Software Engineer") # Replace with your search query
search_box.send_keys(Keys.RETURN)
time.sleep(5)
Step 6: Send Connection Requests
Now we’ll loop through the profiles and send connection requests.
try:
while True: # Loop to process profiles on the current page
# Scroll to load profiles dynamically
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3) # Wait for new profiles to load
# Find all "Connect" buttons
connect_buttons = driver.find_elements(By.XPATH, "//button[.//span[contains(@class, 'artdeco-button__text') and text()='Connect']]")
if not connect_buttons:
print("No 'Connect' buttons found on the current page.")
break # Exit the loop if no buttons are found
for button in connect_buttons:
try:
# Scroll to the "Connect" button to bring it into view
driver.execute_script("arguments[0].scrollIntoView(true);", button)
time.sleep(1) # Allow time for scrolling
# Try normal click
try:
button.click()
except:
# Fallback to JavaScript click if intercepted
driver.execute_script("arguments[0].click();", button)
time.sleep(2) # Allow time for the pop-up to load
# Click the "Send without a note" button in the pop-up
try:
send_without_note_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(@class, 'artdeco-button__text') and text()='Send without a note']]"))
)
send_without_note_button.click()
print("Connection request sent successfully.")
time.sleep(2)
except Exception as send_error:
print(f"Error finding 'Send without a note' button: {send_error}")
except Exception as button_error:
print(f"Error clicking 'Connect' button: {button_error}")
except KeyboardInterrupt:
print("Script stopped by user.")
Step 7: Log Out and Close the Browser
Once all actions are completed, log out and close the browser to end the session.
Python
# Log out (optional)
driver.get("https://www.linkedin.com/login?session_redirect=https%3A%2F%2Fwww.linkedin.com%2Fm%2Flogout%2F")
# Close the browser
driver.quit()
Step 8: Full Code Implementation
Below is the complete Python script for automating LinkedIn connections. You can run this code after ensuring you have set up everything correctly.
Python
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time
# Step 1: Set up the browser
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.linkedin.com/login")
# Step 2: Log in to LinkedIn
username = driver.find_element(By.ID, "username")
password = driver.find_element(By.ID, "password")
username.send_keys("Enter your linkedin email") # Replace with your LinkedIn email
password.send_keys("Enter your password here") # Replace with your LinkedIn password
# Click login button
login_button = driver.find_element(By.XPATH, "//button[@type='submit']")
login_button.click()
time.sleep(3)
# Step 3: Search for profiles using the correct class name for search box
search_box = driver.find_element(By.CSS_SELECTOR, ".search-global-typeahead__input.search-global-typeahead__input--ellipsis")
search_box.send_keys("Software Engineer") # Replace with your search query
search_box.send_keys(Keys.RETURN)
time.sleep(5)
# Step 4: Navigate to the "People" tab using Explicit Wait
try:
# Wait for the "People" button to be clickable
people_tab = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'People')]"))
)
people_tab.click()
print("Navigated to the 'People' section successfully.")
time.sleep(5)
except Exception as e:
print(f"Error navigating to the 'People' section: {e}")
driver.quit()
exit()
# Step 5: Send connection requests
try:
while True: # Loop to process profiles on the current page
# Scroll to load profiles dynamically
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3) # Wait for new profiles to load
# Find all "Connect" buttons
connect_buttons = driver.find_elements(By.XPATH, "//button[.//span[contains(@class, 'artdeco-button__text') and text()='Connect']]")
if not connect_buttons:
print("No 'Connect' buttons found on the current page.")
break # Exit the loop if no buttons are found
for button in connect_buttons:
try:
# Scroll to the "Connect" button to bring it into view
driver.execute_script("arguments[0].scrollIntoView(true);", button)
time.sleep(1) # Allow time for scrolling
# Try normal click
try:
button.click()
except:
# Fallback to JavaScript click if intercepted
driver.execute_script("arguments[0].click();", button)
time.sleep(2) # Allow time for the pop-up to load
# Click the "Send without a note" button in the pop-up
try:
send_without_note_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(@class, 'artdeco-button__text') and text()='Send without a note']]"))
)
send_without_note_button.click()
print("Connection request sent successfully.")
time.sleep(2)
except Exception as send_error:
print(f"Error finding 'Send without a note' button: {send_error}")
except Exception as button_error:
print(f"Error clicking 'Connect' button: {button_error}")
except KeyboardInterrupt:
print("Script stopped by user.")
Output:
Navigated to the 'People' section successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
Connection request sent successfully.
No 'Connect' buttons found on the current page.
The console will show something like this while you can watch all the automated connection request on the browser tab that opened automatically.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4