Python Selenium – Logging in to LinkedIn using a web driver

In this project we will use Selenium and a Chrome web driver to log in to a LinkedIn account. Prerequisite of the project is that you have a LinkedIn account and a Chrome web driver stored on your machine. You may download the web driver by clicking here, note that it needs to match your current version of Chrome.

We begin by specifying the location of our web driver, the driver.get() method will then navigate to the given URL Address. In this example we have set this to ‘https://www.linkedin.com’. We then wait 1 second for the web page to load all elements of the page.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome("C:\Chromium\chromedriver.exe")
driver.get('https://www.linkedin.com')
driver.implicitly_wait(1)

Moving on we start locating the various fields on the login page starting with the username/email field then the password and finally the login button. Using the send.keys() method we can have our account credentials automatically populated. Once entered using the click() method the script clicks on the submit/login button.

username = driver.find_element_by_xpath('//*[@type="text"]')
username.send_keys(Email)
password = driver.find_element_by_xpath('//*[@type="password"]')
password.send_keys(Password)
log_in_button= driver.find_element_by_xpath('//*[@class="sign-in-form__submit-button"]')
log_in_button.click()
driver.implicitly_wait(1)

Full source code can be found below.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#Specifies the location of the web driver
driver = webdriver.Chrome("C:\Chromium\chromedriver.exe")
#The driver.get() method will navigate to the given URL Address
driver.get('https://www.linkedin.com')
#Applying a delay for the web page to load
driver.implicitly_wait(1)

#Locating the email field on the Linkedin login page
username = driver.find_element_by_xpath('//*[@type="text"]')
#Entering in an email address 
username.send_keys(Email)
#Locating the password field on the Linkedin login page
password = driver.find_element_by_xpath('//*[@type="password"]')
#Entering in a password
password.send_keys(Password)
#Locating the Login button on the Linkedin login page
log_in_button= driver.find_element_by_xpath('//*[@class="sign-in-form__submit-button"]')
#Clicking on the Login button
log_in_button.click()
#Applying a delay for the web page to load
driver.implicitly_wait(1)

One thought on “Python Selenium – Logging in to LinkedIn using a web driver

Leave a Reply