Linkedin python bot

It is often hectic to make connections on linkedin by continuously pressing the connect button again and again monotonously. Here is a way to automate it:

Code

from selenium import webdriver
import pyautogui as pag
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('path where u downloaded the driver')
driver.get('https://www.linkedin.com')
time.sleep(2)

#********** LOG IN *************

username = driver.find_element_by_xpath("//input[@name='session_key']")
password = driver.find_element_by_xpath("//input[@name='session_password']")

username.send_keys('your_mailid')
password.send_keys('your_password')
time.sleep(2)

submit = driver.find_element_by_xpath("//button[@type='submit']").click()

#connecting

driver.get(
    "url of the page you want to connect to")
time.sleep(2)
all_buttons=driver.find_elements_by_tag_name("button")
connect_buttons = [btn for btn in all_buttons if btn.text == 'Connect']
time.sleep(2)
for btn in connect_buttons:
    driver.execute_script("arguments[0].click();", btn)
    time.sleep(2)
    send = driver.find_element_by_xpath("//button[@aria-label='Send now']")
    driver.execute_script("arguments[0].click();", send)
    close=driver.find_element_by_xpath("//button[@aria-label='Dismiss']")
    driver.execute_script("arguments[0].click();", close)
    time.sleep(2)

Web driver

Just copy paste this code on your favourite editor, make sure that you name it something as bot.py with the py extension something like that. Now in order to make it work you need to have a webdriver. Since I have used the chrome web driver I will tell you about that. By this I mean that the script that we are executing will only execute on google chrome because we are using the chrome webdriver. We can use others as well, but lets save it for later. You can download the chrome web driver from chromeWebdriver.

Understanding the code

From the code block at the top we basically log into the linkedin account ull need to put in ur mail id and password there.

Go to your linkedin->hit search->click on people->copy the url-> paste the url in "url of the page you want to connect".

Here is the real code now. We create a list of elements with the tagname button this is because when we inspect the page linkedin is clever enough to put unique id for each button.

To click on the button we are using js because linkedin has blocked any activity of python bots.

Run code

Open terminal run the code as python3 name.py