Automate LinkedIn Posts with Selenium And Python

Pius Adams Ijachi
5 min readJan 25, 2021

--

In the spirit of making things easier I decided to automate the process of creating posts on LinkedIn, I had the option of using Selenium or the LinkedIn api , I chose selenium cause just cool seeing chrome work by itself, Hopefully it helps things easier for you ,It certainly did for me.

Before we begin, You’ll need to download and install PYTHON

After that is done we’ll need to install selenium so open up your command prompt and type..

pip install selenium

After installing that we’ll need to download a web driver selenium needs this so it can interact with the browser, There are multiple web drivers for all types of browsers for this tutorial we will use the chrome browser web driver..

screenshot by author

So we have to download the web driver that correlates with the chrome version we have to check or chrome version…

  1. Click on options and then on help
  2. Click on About Google Chrome
screenshot by author
Screenshot by author

Now download the correct version after that go to downloads unzip the folder and copy the chromedriver.exe into Local Disk (c:) → Program Files(x86) and copy the path

screenshot by author

We have to show selenium the path to the chrome driver , it can be in any directory but this makes it more organized ,If you had any problems with the installation check this video out .

Now let’s open up our IDE

screenshot by author

First we import selenium and from selenium we import web driver , Path is were the chrome webdriver.exe is , Then we tell the driver to use the chrome web driver in that path

driver.get() is used to fetch whatever parameter it’s given , in this case we are saying it should get the LinkedIn URL in the browser ( chrome) you can run the code and it should open the URL given

screenshot by author

Now we have to find away to click on the email or phone number so first we inspect it and find out it’s ID

Screenshot

To access the input we need to use the ID and click enter by first we need an import

screenshot
  1. find_element_by_id() means get the element with the id “session_key”
  2. send_keys() will type out whatever it gets into the whatever element it’s attached to
  3. Keys.RETUN will click on field ( so after typing the username it clicks enter and after typing password it clicks on enter)

Running This should log you into LinkedIn and we should see your feed.

Now we should click on Start a post same as before we inspect but before that we should import some things

screenshot
screenshot
screenshot

The imports are what we need to implement waits ,after that we inspect the element this element does not have an ID so we’ll use the CSS SELECTOR

NOTE: an ID is preferred to CSS SELECTOR because ID are usually unique it works in this case but in the future either use ID or XPATH(we’ll use it later)

WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.CSS_SELECTOR, ‘.theme — mercado .share-box-feed-entry__trigger — v2’)))
###THIS LINE OF CODE TELLS THE DRIVER TO WAIT FOR 2 seconds untill it finds an element with a CSS_SELECTOR with the selector we gave (we did this incase of a slow connection you can increase the seconds)
screenshot
screenshot
  1. driver.find_element_by_class_name(“ql-editor”) it gets the element by class name

Create a text.txt file in the same directory and write anything in it

post_reader = open(“text.txt”, “r”)
post = post_reader.read()
###this code reads whatever is written and saves in the post variable

2.modal.send_keys(post) types out whatever it gets from the post variable

So now we have to click the POST button

screenshot
screenshot

We inspect this and because the CSS SELECTOR and CLASS isn’t unique we find the element by full XPATH (which in my opinion is the best way to find a unique element with no ID)..and then we click on enter…DONE!!!!!

Your code should look like this

Ok then is was mainly meant to teach you about the few ways to get an element and also automate a task

Remember use ID or XPATH to get a unique element(one specific element)

--

--

Pius Adams Ijachi
Pius Adams Ijachi

No responses yet