Categories
Development SEO and Growth Hacking

A simple LinkedIn Group Submitter

LinkedIn API doesn’t allow you to publish into groups if you are not their administrator. That was done in order to eliminate spamming, but if you are a member of several groups of a similar topic and you want to share some interesting information with all of those groups, you have to do it manually […]

LinkedInLinkedIn API doesn’t allow you to publish into groups if you are not their administrator. That was done in order to eliminate spamming, but if you are a member of several groups of a similar topic and you want to share some interesting information with all of those groups, you have to do it manually group by group and eventually it becomes tedious. In this post I’ll show you a simple way to automate this process in C# using Selenium WebDriver.

Looking at this LinkedIn Group Submitter’s code, you can learn some useful techniques for using Selenium WebDriver. In this program I use ChromeDriver, and here you can read more about where to get ChromeDriver and how to add it into C# projects.

Let’s start. First of all we need to create and configure a ChromeDriver object:

ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized");

using (var driver = new ChromeDriver(options))
{

In this code I add an option to start Google Chrome window maximized. If you want rather to hide the window, you may use the following options instead:

options.AddArgument("--window-position=-2000,0");
options.AddArgument("--window-size=1920,1080");

The following line forces the WebDriver to wait for 3 seconds until elements appear on the page instead of throwing an exception immediately. This may be necessary if web elements occur on the page dynamically (LinkedIn website actively uses them):

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3));

Now let’s sign in into LinkedIn:

driver.Navigate().GoToUrl("https://www.linkedin.com/");
driver.FindElement(By.Id("session_key-login")).SendKeys("<USER EMAIL>");
driver.FindElement(By.Id("session_password-login")).SendKeys("<USER PASSWORD>");
driver.FindElement(By.Id("login")).Submit();

I think this code speaks for itself. First it opens the LinkedIn home page, then it types the user’s email and password (don’t forget to put in your own) and then submits the login form.

Then, since we have logged into LinkedIn, we need to open the group’s page and enter our text into the fields of submit form:

LinkedIn Form

The next piece of code opens LinkedIn Group’s page, clicks on post title (1) and waits until the details area (2) and other elements appear:

driver.Navigate().GoToUrl("http://www.linkedin.com/groups/scrapingpro-6643092");
driver.FindElement(By.Id("postTitle-ANetPostForm")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(1)).Until(drv => drv.FindElement(By.Id("postText-ANetPostForm")));

Now let’s type the title of our post in:

driver.FindElement(By.Id("postTitle-ANetPostForm")).SendKeys("<POST TITLE>");
driver.FindElement(By.Id("postText-ANetPostForm")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(drv => drv.FindElement(By.Id("share-view")).GetCssValue("display") != "none");

Note that after typing the title this code clicks on details area (2) and waits 3 seconds until element with id=”share-view” becomes visible. This is necessary to give LinkedIn an opportunity to build a preview with a snippet and featured image from your website if you specified its URL in the title. Like this:

LinkedIn Preview

Note also that the title can’t be longer than 200 characters. If you need to add more text you can type it into the details window in the following way:

driver.FindElement(By.Id("postText-ANetPostForm")).SendKeys("<MORE DETAILS>");

After we fill all the fields out, we can submit the form by clicking the submit button (3):

driver.FindElement(By.Id("share-form")).Submit();

That’s it!

Here is the complete code of this simple LinkedIn Group Submitter for your convenience:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.IO;
using System.Threading;

namespace LinkedInSubmitter
{
    class Program
    {
        static void Main(string[] args)
        {
            var groups = new[] 
            { 
               "http://www.linkedin.com/groups/scrapingpro-6643092" 
            };
            var user = "your@email.com";
            var password = "your LinkedIn password";
            var title = "Post title";
            var details = "Post details";

            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--window-position=-2000,0");
            options.AddArgument("--window-size=1920,1080");

            using (var driver = new ChromeDriver(options))
            {
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3));

                driver.Navigate().GoToUrl("https://www.linkedin.com/");
                driver.FindElement(By.Id("session_key-login")).SendKeys(user);
                driver.FindElement(By.Id("session_password-login")).SendKeys(password);
                driver.FindElement(By.Id("login")).Submit();

                foreach (var group in groups)
                {
                    driver.Navigate().GoToUrl(group);
                    driver.FindElement(By.Id("postTitle-ANetPostForm")).Click();
                    new WebDriverWait(driver, TimeSpan.FromSeconds(1)).Until(drv => drv.FindElement(By.Id("postText-ANetPostForm")));

                    driver.FindElement(By.Id("postTitle-ANetPostForm")).SendKeys(title);
                    driver.FindElement(By.Id("postText-ANetPostForm")).Click();
                    new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(drv => drv.FindElement(By.Id("share-view")).GetCssValue("display") != "none");

                    driver.FindElement(By.Id("postText-ANetPostForm")).SendKeys(details);
                    driver.FindElement(By.Id("share-form")).Submit();
                }
            }
        }
    }
}

 

 

One reply on “A simple LinkedIn Group Submitter”

Leave a Reply to Igor Savinkin Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.