-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (51 loc) · 2.27 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class LinkedinBot:
def __init__(self, username, password):
""" Initialized Chromedriver, sets common urls, username and password for user """
self.driver = webdriver.Chrome('./chromedriver.exe')
self.base_url = 'https://www.linkedin.com'
self.login_url = self.base_url + '/login'
self.feed_url = self.base_url + '/feed'
self.username = username
self.password = password
def _nav(self, url):
self.driver.get(url)
time.sleep(3)
def login(self, username, password):
""" Login to LinkedIn account """
self._nav(self.login_url)
self.driver.find_element_by_id('username').send_keys(self.username)
self.driver.find_element_by_id('password').send_keys(self.password)
self.driver.find_element_by_xpath("//button[contains(text(), 'Sign in')]").click()
def post(self, text):
""" Make a text post """
self.driver.find_element_by_class_name('share-box__open').click()
self.driver.find_element_by_class_name('mentions-texteditor__content').send_keys(text)
self.driver.find_element_by_class_name('share-actions__primary-action').click()
def search(self, text, connect=False):
""" Search execeuted from home screen """
self._nav(self.feed_url)
search = self.driver.find_element_by_class_name('search-global-typeahead__input')
search.send_keys(text)
search.send_keys(Keys.ENTER)
# Waiting for search results to load
time.sleep(3)
if connect:
self._search_connect()
def _search_connect(self):
""" Called after search method to send connections to all on page """
connect = self.driver.find_element_by_class_name('search-result__action-button')
connect.click()
time.sleep(2)
self.driver.find_element_by_class_name('ml1').click()
if __name__ == '__main__':
username = ''
password = ''
post_text = ''
search_text = ''
bot = LinkedinBot(username, password)
bot.login(username, password)
bot.post(post_text)
bot.search(search_text, connect=True)