Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# /bin/usr/python
import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.opera.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
class Bot():
def __init__(self):
options = Options()
options.add_argument("window-size=1920,1200")
# You can use a different web browser
self._driver = webdriver.Opera(executable_path=r'D:\\Program Files\\operadriver.exe', options=options)
self._sent_messages = []
self._sent_names = []
@property
def driver(self):
return self._driver
def start(self):
driver = self.driver
driver.get('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
creds = {
"email": "your_email_here",
"pass": "your_password_here"
}
email_input = driver.find_element_by_id('username')
pass_input = driver.find_element_by_id('password')
email_input.send_keys(creds['email'])
pass_input.send_keys(creds['pass'])
submit_element = driver.find_element_by_xpath("//button[@data-litms-control-urn='login-submit']")
submit_element.click()
driver.implicitly_wait(4)
network_link = driver.find_element_by_xpath("//a[@href='/mynetwork/']")
network_link.click()
for i in range(100):
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "discover-entity-type-card__info-container"))
)
finally:
driver.execute_script("window.scrollTo(0, 400)")
driver.execute_script("return document.getElementById('msg-overlay').remove();")
driver.implicitly_wait(4)
for _ in range(0, 5):
self.search_loop()
driver.refresh()
driver.save_screenshot('screen.png')
self.write_report()
driver.quit()
def search_loop(self):
elements = [elem for elem in self.driver.find_elements_by_xpath("//li[contains(@class, 'discover-entity-card--default-width')]") if elem.is_displayed()]
for element in elements:
try:
name = element.find_element_by_class_name('discover-person-card__name')
job = element.find_element_by_class_name('discover-person-card__occupation')
connect_button = element.find_element_by_xpath(f"//button[@aria-label='Zaproś użytkownika {name.text} do nawiązania kontaktu']")
print('PROFILE')
print(name.text)
print('-------')
print(job.text)
if not self.is_recruiter(job.text) or name.text in self._sent_names:
continue
ActionChains(self.driver).move_to_element(connect_button).perform()
self.driver.implicitly_wait(5)
connect_button.click()
self._sent_messages.append(f"{name.text} - {job.text}\n")
self._sent_names.append(name.text)
except NoSuchElementException:
break
self.scroll_to_bottom()
def write_report(self):
pass
# Of course your citeria may differ
def is_recruiter(self, job):
for word in ['recruiter', 'Recruiter', 'Talent', 'Headhunter', 'Rekruter', 'rekruter', 'Recruitment']:
if word in job:
return True
return False
def scroll_to_bottom(self):
self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
self.driver.implicitly_wait(3)
Bot().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.