-
Notifications
You must be signed in to change notification settings - Fork 3
/
Linkedin_auto_connector_bot.py
217 lines (181 loc) · 8.72 KB
/
Linkedin_auto_connector_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
This script is a LinkedIn bot that automatically sends connection requests with a custom note to profiles on LinkedIn.
It uses the Selenium WebDriver to navigate LinkedIn and interact with the UI elements.
Do 100 requests per week!!!!!
If not, LinkedIn will block your account.
Add my LinkedIn also - https://www.linkedin.com/in/mrbondarenko/
Replace your search link with keywords you need!
Go to LinkedIn main page, press on the search bar, put the keywords you need(Tech Recruter or Cloud Engineer for example),
press enter, select people only! copy the link and paste it in the SEARCH_LINK variable.
Have fun!
"""
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException, MoveTargetOutOfBoundsException, ElementClickInterceptedException
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.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
import logging
import time
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Replace with your LinkedIn credentials
LINKEDIN_USERNAME = '#@yahoo.com' # your email
LINKEDIN_PASSWORD = '' # your password
MAX_RETRIES = 5 # Maximum number of retries for refreshing
SEARCH_LINK = ("https://www.linkedin.com/search/results/people/?geoUrn=%5B%22103644278%22%5D&keywords=technical%20recruiter&origin=GLOBAL_SEARCH_HEADER&page=14")
# Base connection message template
BASE_CONNECTION_MESSAGE = """Hi there,
I hope this message finds you well. I'm exploring new opportunities in tech and would love to connect. I have a strong background in Azure Cloud and Software Development, with a focus on Cloud Engineering and AI/ML.
Looking forward to connecting!
Best regards,
Pavlo Bondarenko
"""
MAX_CONNECT_REQUESTS = 20 # Limit for connection requests
def login_to_linkedin(driver, username, password):
try:
driver.get("https://www.linkedin.com/login")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "username")))
# Enter username
username_field = driver.find_element(By.ID, "username")
username_field.send_keys(username)
# Enter password
password_field = driver.find_element(By.ID, "password")
password_field.send_keys(password)
password_field.send_keys(Keys.RETURN)
time.sleep(5) # Wait for the page to load or enter captcha
WebDriverWait(driver, 20).until(EC.url_contains("/feed"))
logging.info("Successfully logged into LinkedIn.")
time.sleep(5) # Wait for the feed to load
except Exception as e:
logging.error(f"Error during LinkedIn login: {e}")
def go_to_next_page(driver):
try:
time.sleep(5) # Wait for the page to load
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Scroll down
next_page_button = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//button[@aria-label='Next']"))
)
next_page_button.click()
logging.info("Navigated to the next page")
time.sleep(5) # Wait for the new page to load
except NoSuchElementException as e:
logging.error(f"Element not found: {e}")
return False
except Exception as e:
logging.error(f"Error navigating to the next page: {e}")
return False
return True
def scrool_down(driver):
try:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Scroll down
time.sleep(5) # Wait for the page to load
except Exception as e:
logging.error(f"Error during scrolling down: {e}")
def handle_connect_button_with_retry(driver, button):
retry_count = 0
while retry_count < MAX_RETRIES:
try:
button.click()
time.sleep(2)
add_note_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[@aria-label='Add a note']"))
)
add_note_button.click()
time.sleep(2)
message_box = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//textarea[@name='message']"))
)
message_box.send_keys(BASE_CONNECTION_MESSAGE)
time.sleep(2)
send_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//span[contains(@class, 'artdeco-button__text') and text()='Send']"))
)
send_button.click()
logging.info("Sent connection request with a custom note.")
time.sleep(2)
return # Exit the function if successful
except ElementClickInterceptedException as e:
logging.error(f"Error: Element not clickable, retrying... {e}")
retry_count += 1
if not refresh_page(driver, retry_count): # Try refreshing the page
logging.error("Unable to resolve the error after retries. Exiting.")
break
except Exception as e:
logging.error(f"Error handling 'Connect' button: {e}")
break
def handle_follow_button(button):
try:
button.click()
logging.info("Followed the user.")
time.sleep(1)
except Exception as e:
logging.error(f"Error handling 'Follow' button: {e}")
def process_buttons(driver):
try:
# Navigate to the search page
driver.get(SEARCH_LINK)
scrool_down(driver)
time.sleep(5)
connect_requests_sent = 0
working = True
while working:
# Find all buttons on the page
buttons = driver.find_elements(By.TAG_NAME, "button")
# Count "Connect" and "Follow" buttons
connect_buttons_count = sum(1 for button in buttons if button.text.strip().lower() == "connect")
follow_buttons_count = sum(1 for button in buttons if button.text.strip().lower() == "follow")
logging.info(f"Total 'Connect' buttons on the page: {connect_buttons_count}")
logging.info(f"Total 'Follow' buttons on the page: {follow_buttons_count}")
# Process each "Connect" and "Follow" button
for button in buttons:
button_text = button.text.strip().lower()
if button_text == "connect" and connect_requests_sent < MAX_CONNECT_REQUESTS:
handle_connect_button_with_retry(driver, button)
connect_requests_sent += 1
if connect_requests_sent >= MAX_CONNECT_REQUESTS:
logging.info(
f"Reached the limit of {MAX_CONNECT_REQUESTS} connection requests. Stopping connection requests.")
working = False
break
time.sleep(5)
elif button_text == "follow":
handle_follow_button(button)
time.sleep(5)
# Attempt to navigate to the next page
if not go_to_next_page(driver):
logging.info("No more pages to process. Exiting.")
break
# Scroll down to load all elements on the new page
scrool_down(driver)
time.sleep(5)
except Exception as e:
logging.error(f"Error while processing buttons: {e}")
def refresh_page(driver, retries):
for attempt in range(1, retries + 1):
try:
logging.info(f"Attempt {attempt}/{retries}: Refreshing the page.")
driver.refresh() # Refresh the page
time.sleep(5) # Wait for the page to reload
return True
except Exception as e:
logging.error(f"Error during page refresh: {e}")
if attempt == retries:
logging.error("Maximum retries reached. Exiting the program.")
driver.quit()
exit(1)
return False
if __name__ == "__main__":
options = Options()
options.binary_location = 'C:/Program Files/Mozilla Firefox/firefox.exe' ## path to your firefox browser(must install firefox browser)
# Set up the webdriver (Replace the path with the path to your webdriver) // mine is geckodriver32.exe already installed in the directory
# go to https://github.com/mozilla/geckodriver/releases to download latest version of geckodriver
service = Service('geckodriver32.exe')
driver = webdriver.Firefox(service=service, options=options)
try:
login_to_linkedin(driver, LINKEDIN_USERNAME, LINKEDIN_PASSWORD)
process_buttons(driver)
finally:
driver.quit()