Python-d01-selenium

Here is an example to use selenium in python on linkedin.
This code will connect to linkedin and send message “testing” to the first friend
You have to change the username and password to do the authentication

this code is working in python 3.8 and selenium 4.14.0

1
2
3
4
5
6
7
8
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

import time

driver = webdriver.Chrome(service=Service(executable_path='C:\dev\softwares\chromedriver\chromedriver.exe'))
driver.get('https://www.linkedin.com/')
1
2
3
4
5
6
7
8
9
time.sleep(2)

username = driver.find_element(By.XPATH, "//input[@name='session_key']")
password = driver.find_element(By.XPATH, "//input[@name='session_password']")
username.send_keys(USERNAME_TO_CHANGE)
password.send_keys(PASSWORD_TO_CHANGE)

time.sleep(2)
submit = driver.find_element(By.XPATH, "//button[@type='submit']").click()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
driver.get("https://www.linkedin.com/search/results/people/?network=%5B%22F%22%5D&origin=FACETED_SEARCH")
time.sleep(2)

all_buttons = driver.find_elements(By.TAG_NAME, 'button')
message_buttons = [btn for btn in all_buttons if btn.text == "Message"]

message_buttons[0].click()

time.sleep(2)

main_div = driver.find_element(By.XPATH, "//div[starts-with(@class, 'msg-form__msg-content-container')]")
main_div.click()

time.sleep(2)

paragraphs = driver.find_elements(By.TAG_NAME, "p")
paragraphs[-5].send_keys("testing")