Hvordan jeg oprettede en LinkedIn-kontakt ved at tilføje bot - og faktisk fik et par interviews med det

På LinkedIn er der et afsnit med titlen "Folk, du måske kender." Det er under fanen Mit netværk .

Dette er den side, der foreslår personer, du måske vil oprette forbindelse til.

Du kan klikke på disse Connect- knapper for at sende forbindelsesanmodninger til personerne på denne liste.

For et par år siden fandt jeg denne side, og jeg begyndte tilfældigt at tilføje folk der. Jeg ville bare klikke på forbindelsesknappen på hver enkelt person, jeg fandt på denne side.

Jeg regnede bare med, at det kunne være nyttigt at have mange forbindelser på LinkedIn for at få de slags job, jeg ønskede at få, for eksempel praktikophold til softwareingeniør.

Men efter et stykke tid blev det lidt besværligt at fortsætte med at klikke på disse forbindelsesknapper manuelt.

Så jeg besluttede at lave en lille bot for at klikke på disse knapper for mig.

Dette er en artikel om, hvordan jeg lavede denne bot, hvad der skete som et resultat, og hvad jeg lærte af den.

Hvordan jeg lavede bot

De værktøjer, jeg brugte

Jeg lavede denne enkle bot for at tilføje tilfældige personer på LinkedIn med JavaScript og Greasemonkey .

Greasemonkey er en Firefox-tilføjelse, der hjælper dig med at administrere brugerdefineret JavaScript-kode.

Med det kan du indstille tingene, så et bestemt sæt kode kører automatisk, når du åbner en bestemt URL.

Du kan også gemme nogle data i Greasemonkey. Jeg brugte denne funktion til at holde styr på antallet af personer, jeg tilføjede med denne bot. På den måde var jeg i stand til at holde styr på dette nummer konsekvent, selv når jeg lukkede browseren eller opdaterede siden.

Koden jeg brugte

Desværre opbevarede jeg ikke den kode, jeg brugte til at oprette min bot, efter at jeg havde brugt den.

Så i denne artikel vil jeg gøre mit bedste for at genskabe det så tæt som muligt.

Først for at oprette denne bit kode brugte jeg Google Chrome. Senere skiftede jeg til Firefox for at bruge Greasemonkey, som jeg nævnte tidligere. Jeg valgte først at bruge Chrome bare fordi jeg var mere vant til det.

Lad os nu gå igennem hvordan jeg genskaber denne kode i dag. I denne artikel vil jeg kun vise dig kernefunktionaliteten i denne bot - for at tilføje folk - bare for at holde det enkelt. Så jeg vil springe over delen om at bruge Greasemonkey til at gemme data vedvarende her.

Giv mig besked i kommentarerne, hvis du vil have mig til at dække den del i en separat artikel.

Trin 0: Grundlæggende JavaScript

Hvis du ikke er alt for fortrolig med JavaScript, lad os hurtigt gennemgå nogle af de grundlæggende JavaScript her.

Vi bruger Google Chrome her, men du kan bruge enhver browser, du gerne vil bruge.

Åbn først ethvert websted, lad os sige, Google.com.

Derefter skal du åbne browserens JavaScript-konsol der.

På Google Chrome kan du gøre det på et par forskellige måder.

Sådan som jeg normalt gør det er følgende:

  • Højreklik et vilkårligt sted på siden.
  • Klik derefter på Inspicer ud af den menu, der dukker op.
  • Når du klikker på det, skal et vindue som det følgende vises.
  • Klik derefter på fanen Konsol der for at få vist JavaScript-konsollen.
  • Når du klikker på fanen Konsol , skal du se JavaScript-konsollen.

Det er her, du kan indtaste enhver JavaScript-kode for at teste den. Du kan bruge den kode, du indtaster, til at interagere med den side, der er åben i din browser.

Prøv for eksempel at indtaste følgende kode i konsollen og tryk på Enter.

selected = document.querySelector('body');

Dette vælger body tag på den side, der er åben i browseren. Derefter tildeler den den til en ny variabel kaldet valgt .

I Chrome og Firefox er der en stenografi for:

selected = document.querySelector('body');

I stedet kan du bare skrive:

selected = $('body');

Denne kode svarer til den ovenfor.

Jeg vil bruge denne stenografiske notation med dollartegnet i hele denne artikel for at holde vores kode kort og enkel.

Du skal heller ikke bekymre dig om det, hvis du endnu ikke kender det grundlæggende i HTML og JavaScript. Jeg vil gøre mit bedste for at skrive denne artikel, så den er let at forstå, selv for begyndere.

Hvis du ikke er interesseret i den kode, jeg vil vise dig, kan du også bare springe til sektionerne om, hvad der skete, og hvad jeg lærte af denne oplevelse i slutningen.

Lad os nu gå gennem vores bots kode trin for trin.

Trin 1: Find målelementet

Først skal du skrive den bit kode, der finder de knapper, du vil klikke på.

Log ind på LinkedIn. Gå derefter til fanen Mit netværk. Det er i øjeblikket på //www.linkedin.com/mynetwork/ (juli 2018).

Du skal kunne finde sektionen Mennesker, du måske kender der.

Højreklik derefter på Chrome på knappen "Opret forbindelse" på en af ​​de anbefalede personer der på Chrome. Klik derefter på Inspicer .

Når du har gjort det, vil det element, som du lige har klikket på, blive fremhævet i udviklervinduet.

Dette er HTML-koden, der er fremhævet med blåt her:

Connect

Dette er en span- fane, der viser teksten: Connect . Hvad vi virkelig vil klikke på er ikke denne, men dens overordnede element, som er en knap.

Du kan finde det lige over det span-element, som vi valgte.

Lad os nu undersøge dette knapelement:

 Connect  Invite Azul Pinochet Barros to connect 

Der er en masse ting her, men her er den vigtige del:

 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.

Som altid, hvis du har spørgsmål om dette eller andet, er du velkommen til at fortælle mig i en kommentar nedenfor eller på Instagram eller Twitter (@ykdojo på begge).

Tak fordi du læste denne artikel!