Comment j'ai créé un contact LinkedIn en ajoutant un bot - et j'ai en fait eu quelques interviews avec lui

Sur LinkedIn, il existe une section intitulée "Personnes que vous connaissez peut-être". C'est sous l' onglet Mon réseau .

C'est la page qui suggère les personnes avec lesquelles vous souhaitez vous connecter.

Vous pouvez cliquer sur ces Connect boutons pour envoyer des demandes de connexion aux personnes dans cette liste.

Il y a quelques années, j'ai trouvé cette page et j'ai commencé à y ajouter des personnes au hasard. Je voudrais simplement cliquer sur le bouton de connexion de chaque personne que j'ai trouvée sur cette page.

J'ai juste pensé qu'il pourrait être utile d'avoir beaucoup de connexions sur LinkedIn pour obtenir le type d'emplois que je voulais obtenir, par exemple, des stages d'ingénieur logiciel.

Mais après un certain temps, il est devenu un peu difficile de continuer à cliquer manuellement sur ces boutons de connexion.

J'ai donc décidé de créer un petit robot pour cliquer sur ces boutons à ma place.

Ceci est un article sur la façon dont j'ai créé ce bot, ce qui s'est passé en conséquence et ce que j'en ai appris.

Comment j'ai fait le bot

Les outils que j'ai utilisés

J'ai créé ce robot simple pour ajouter des personnes au hasard sur LinkedIn avec JavaScript et Greasemonkey .

Greasemonkey est un module complémentaire de Firefox qui vous aide à gérer le code JavaScript personnalisé.

Avec lui, vous pouvez configurer les choses pour qu'un certain ensemble de code s'exécute automatiquement lorsque vous ouvrez une certaine URL.

Vous pouvez également stocker certaines données dans Greasemonkey. J'ai utilisé cette fonctionnalité pour suivre le nombre de personnes que j'ai ajoutées avec ce bot. De cette façon, j'ai pu garder une trace de ce nombre de manière cohérente même lorsque j'ai fermé le navigateur ou actualisé la page.

Le code que j'ai utilisé

Malheureusement, je n'ai pas conservé le code que j'ai utilisé pour créer mon bot après l'avoir utilisé.

Donc, dans cet article, je ferai de mon mieux pour le recréer le plus fidèlement possible.

Au départ, pour créer ce morceau de code, j'ai utilisé Google Chrome. Plus tard, je suis passé à Firefox pour utiliser Greasemonkey, que j'ai mentionné plus tôt. J'ai choisi d'utiliser Chrome au départ simplement parce que j'y étais plus habitué.

Maintenant, voyons ensemble comment je recréerais ce code aujourd'hui. Dans cet article, pour rester simple, je vais seulement vous montrer la fonctionnalité de base de ce bot - ajouter des personnes. Donc, je vais sauter la partie sur l'utilisation de Greasemonkey pour stocker des données de manière persistante ici.

S'il vous plaît laissez-moi savoir dans les commentaires si vous souhaitez que je couvre cette partie dans un article séparé.

Étape 0: principes de base de JavaScript

Si vous n'êtes pas trop familier avec JavaScript, passons rapidement en revue quelques bases de JavaScript ici.

Nous allons utiliser Google Chrome ici, mais vous pouvez utiliser n'importe quel navigateur que vous souhaitez utiliser.

Tout d'abord, ouvrez n'importe quel site Web, disons Google.com.

Ensuite, vous devrez y ouvrir la console JavaScript du navigateur.

Sur Google Chrome, vous pouvez le faire de différentes manières.

La façon dont je le fais habituellement est la suivante:

  • Cliquez avec le bouton droit n'importe où sur la page.
  • Ensuite, cliquez sur Inspecter dans le menu qui s'affiche.
  • Lorsque vous cliquez dessus, une fenêtre comme celle-ci devrait apparaître.
  • Ensuite, cliquez sur l' onglet Console pour afficher la console JavaScript.
  • Une fois que vous avez cliqué sur l' onglet Console , vous devriez voir la console JavaScript.

C'est ici que vous pouvez taper n'importe quel code JavaScript pour le tester. Vous pouvez utiliser le code que vous entrez pour interagir avec la page ouverte dans votre navigateur.

Par exemple, essayez de taper le code suivant dans la console et appuyez sur Entrée.

selected = document.querySelector('body');

Cela sélectionne la balise de corps dans la page qui est ouverte sur le navigateur. Ensuite, il l'assigne à une nouvelle variable appelée selected .

Dans Chrome et Firefox, il existe un raccourci pour:

selected = document.querySelector('body');

Au lieu de cela, vous pouvez simplement écrire:

selected = $('body');

Ce code est équivalent à celui ci-dessus.

Je vais utiliser cette notation abrégée avec le signe dollar tout au long de cet article pour garder notre code court et simple.

Ne vous inquiétez pas non plus si vous ne connaissez pas encore les bases du HTML et du JavaScript. Je ferai de mon mieux pour écrire cet article afin qu'il soit facile à comprendre même pour les débutants.

Si le code que je vais vous montrer ne vous intéresse pas, vous pouvez également passer aux sections sur ce qui s'est passé et ce que j'ai appris de cette expérience à la fin.

Now, let’s walk through our bot’s code, step by step.

Step 1: Find the target element

First, you’ll need to write the bit of code that finds the buttons that you want to click.

First, log in to LinkedIn. Then, go to the My Network tab. It’s currently at //www.linkedin.com/mynetwork/ (July, 2018).

You should be able to find the People you may know section there.

Then, on Chrome, right click on the “connect” button on one of the recommended people there. Then, click Inspect.

Once you do so, the element that you just clicked on will be highlighted in the developer window.

This is the HTML code that’s highlighted in blue here:

Connect

This is a span tab that shows the text: Connect. What we really want to click on is not this one, but its parent element, which is a button.

You can find it right above the span element that we selected.

Let’s now examine this button element:

 Connect  Invite Azul Pinochet Barros to connect 

There’s a bunch of stuff here, but here’s the important part:

 Connect ...

Basically, this is a button element whose attribute, data-control-name, is “invite”.

In our script, all we need to do is select elements like this and click them.

You can select these elements with this piece of code:

selected = $(“button[data-control-name=invite]”);

This reads as, select all the button elements whose data-control-name is “invite”.

NOTE: It looks like LinkedIn’s website uses jQuery. So, the notation above is actually a jQuery selector, not a helper function defined by Chrome. Confusingly, their behaviours are slightly different ?

Anyway, once you run this code in your Chrome console, you should be able to see that the correct elements have been selected.

Now, with this piece of code - selected = $("button[data-control-name=invite]"); - your browser finds multiple button elements and puts them in an array. To pick the first one, you can just select the first element in this array like so:

toClick = $("button[data-control-name=invite]")[0];

Then, you can click it with this:

toClick.click();

If it goes through, you should see a confirmation window popping up.

Step 2: Loop through multiple target elements

Now, the next step is to loop through multiple target elements to click so we can add multiple people.

After some experimentation, I realized that there’s a simpler way to select multiple buttons and loop through them than the one I showed earlier.

Here’s how I would do it.

First, use Inspect Element to analyze the structure of this page a bit more. Then, you should be able to see that the people you may knowis just an unordered list.

You should be able to find code that looks like this:

The parent element is a ul (unordered list) element. Its children are li (list item) elements.

Each li element represents each of the people you may know cards you see on the screen.

By selecting these li elements instead of selecting the buttons directly, it actually becomes easier to go through multiple people.

You can select this ul element, the parent of the li elements, like this:

ul = $('ul.mn-pymk-list__cards')[0];

This says, select the ul element with the class ul.mn-pymk-list__cards. We need to add [0] at the end because the raw result is an array containing a single element.

Then, you can select the first li element (the first person’s card) under the ul element like this:

firstLi = ul.querySelector('li');

We don’t need to add [0] at the end of this statement because the querySelector() function only returns one element.

Then, out of firstLi, you can select the button that we need to click like this:

buttonToClick = firstLi.querySelector("button[data-control-name=invite]");

After clicking this button with buttonToClick.click(), we should remove this li element so we can go to the next li element (the next person’s card). We can do that with this:

ul.removeChild(firstLi);

Putting them all together, and putting everything in a while loop, you’ll get something like this:

ul = $('ul.mn-pymk-list__cards')[0];firstLi = ul.querySelector('li');while(firstLi){ // do this while firstLi still exists. buttonToClick = firstLi.querySelector("button[data-control-name=invite]"); ul.removeChild(firstLi); firstLi = ul.querySelector('li');}

This code should work, but it has several issues.

  1. We add people really fast with this, so it’s going to be hard to know what’s going on when you run this code.
  2. We are not keeping track of how many people we’ve added.
  3. We are assuming that buttonToClick is always the correct button to click. Sometimes this button has the text “Invite” instead of “Connect”. We don’t want to click on too many of those “Invite” buttons.

Step 3: Refine our code

I’ve fixed all of the issues I mentioned above and put together a relatively simple piece of code below.

It’s also here on Gist. Perhaps it’s easier to read there.

// this function allows us to stop our code for |ms| milliseconds.function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms));}
// I've put our main code into this function.async function addPeople() { ul = $('ul.mn-pymk-list__cards')[0]; firstLi = ul.querySelector('li'); count = 0; // this is the count of how many people you've added while(firstLi && count < 100){ // stop after adding 100 people buttonToClick = firstLi.querySelector("button[data-control-name=invite]"); // make sure that this button contains the text "Connect" if (buttonToClick.innerText.includes("Connect")){ buttonToClick.click(); count += 1; console.log("I have added " + count + " people so far."); } ul.removeChild(firstLi); await sleep(1000); // stop this function for 1 second here. firstLi = ul.querySelector('li'); }}
addPeople();

If you examine this code carefully, you should be able to notice the couple of changes I’ve made:

  1. I’ve put our code into an async function called addPeople(). In this function, every time we add someone, we pause for 1 second with the sleep() function. More about this pattern here.
  2. I added a count variable to keep track of how many people we’ve added.
  3. I added this if statement: if (buttonToClick.innerText.includes("Connect"){...}. This way, we can make sure that the button we’re clicking contains the word “Connect” inside it.

With these changes, when I run this code, it looks like this:

Step 4: Make further improvements!

On top of what I showed above, I had a few more functionalities when I actually used my bot to add a bunch of people on LinkedIn.

First of all, I used Greasemonkey, which I mentioned earlier, to keep track of the total number of people I’ve added.

Also, to avoid being detected as a bot by LinkedIn, I added a few things:

  1. I randomized the order in which I added people.
  2. I randomized the amount of time I waited every time I added a new person.

I’ll leave all of these as exercise problems for you to solve in case you’re interested in solving them ?

What happened

With my script, I ended up adding 2000+ connections. Then, if I remember correctly, about 400 of them added me back.

As a result, I went from about 300 connections to 700+ connections within a week or so!

Then, after a while, I got banned by LinkedIn from adding any more people. I didn’t know that I could get banned! I was scared for a bit, but the ban lifted after 2 months or so.

More importantly, I was able to land a few interviews from those 400+ new connections. One of the interviews was with this company called Palantir.

Here’s a screenshot of the message I received from them:

What I learned from this experience

I thought what I was doing was pretty silly at the time, but I ended up learning a lot from this experience.

Takeaway #1

First of all, through this experience, I realized that LinkedIn actually works for getting jobs. I was able to get a few job interviews with my bot, after all.

Then, after a while, I also realized that adding thousands of random people was not the most efficient way to use LinkedIn. With that kind of approach, you end up adding a lot of people you don’t need to add.

So, after that experience, I changed my approach to a more focused one.

With my new approach, I would only add recruiters of the companies I wanted to work at. Then, I would only send messages to the people who added me back.

It turned out to be a much more focused, effective strategy to use LinkedIn. With this new strategy, I was able to get a few more job interviews with multiple tech companies, including Yelp and Xamarin. This time, I didn’t have to add thousands of new connections to achieve this result ?

NOTE: I talk more about this strategy in this article, just in case you’re curious about it.

Takeaway #2

Having fun is the best way to hone your programming skills!

Through this particular project, I was able to hone my JavaScript skills. What I learned included:

  • How to set a timed interval between function executions
  • How to select certain HTML elements with JavaScript
  • How to store data locally with Greasemonkey

I learned these things through this project, and it didn’t feel like studying at all because it was so much fun.

Takeaway #3

From this experience, I’ve learned that it sometimes pays to do something weird. So, don’t be afraid of being a little bit mischievous and adventurous if you have any inclination to do so.

Even after this little experiment, I continued to do weird things for fun.

For example, when I was interning at Microsoft, I ran a little experiment where I “stole” a bunch of employee passwords. I did that by sending out a phishing email. It was supposed to be a huge give-away raffle with prizes like Xbox and Surface laptops. It was my hackathon project there.

I also started a programming-education YouTube channel, and eventually decided to work on it full-time and quit my full-time software engineer job.

Perhaps all of these things seemed a little bit weird to other people. But every time I went through each of these experiences, I learned something new, and I had tons of fun along the way. I would say the last one even made my career.

So again, don’t be afraid of trying something strange just for fun! You might learn something valuable along the way.

Okay, that’s it for this article.

This was supposed to be sort of a fun article, but I usually write about more serious stuff.

For example, I have articles about writing your software engineer resume, the best ways to apply for software engineer jobs, and how to get a job at a top tech company.

Feel free to check them out. They are all here on Medium.

Also, as always, if you have any questions about this or anything else, please feel free to let me know in a comment below or on Instagram or Twitter (@ykdojo on both).

Thank you for reading this article!