Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # selenium imports
- import time
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium.webdriver.support.expected_conditions import *
- # filehandling #
- import os.path
- # date time #
- from datetime import date
- # check if the html element is available
- # Marco de Boer (c) 2020
- # version 1 : new
- def isAvailable(available):
- if len(driver.find_elements_by_xpath(available)) > 0:
- return True
- else:
- return False
- # doTheJob, the title is clear I think, it does the job
- # Marco de Boer (c) 2020
- # version 1 : new
- def doTheJob():
- driver.implicitly_wait(10)
- createLogFile('doTheJob started : ' + str(date.today()))
- amountXPath = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
- '2]/div/div/div/div/div[2]/div/div[1]/div/div[1]/div/span '
- time.sleep(10)
- sAmount = driver.find_element_by_xpath(amountXPath).text
- sAmount = sAmount.split()[0]
- amount = int(sAmount)
- print('Amount of requests : ' + str(amount))
- print('===========================')
- try:
- if amount > 0:
- while amount > 0:
- requesterName = '/html[1]/body[1]/div[1]/div[3]/div[1]/div[1]/div[2]/div[2]/div[2]/div[1]/div[3]/div[' \
- '1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[2]/ul[1]/li[1]/div[1]/div[' \
- '2]/div[1]/div[1]/div[2]/a[1] '
- available = isAvailable(requesterName)
- Available(available, requesterName, 'Requester name : ')
- whyPython = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
- '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[4]/ul/li[1]/text '
- available = isAvailable(whyPython)
- Available(available, whyPython, 'Why python : ')
- experience = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
- '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[4]/ul/li[2] '
- available = isAvailable(experience)
- Available(available, experience, 'Experience : ')
- approve = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
- '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[5] '
- available = isAvailable(approve)
- Available(available, approve, 'Ok to the rules : ')
- if approve == 'Akkoord':
- ok = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
- '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[1]/button[1] '
- driver.find_element_by_xpath(ok).click()
- else:
- cancel = '/html/body/div[1]/div[3]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div[' \
- '2]/div/div/div/div/div[2]/div/div[2]/ul/li[1]/div/div[2]/div/div/div[1]/button[2] '
- driver.find_element_by_xpath(cancel).click()
- amount -= 1
- amount = checkAmount(amount, amountXPath)
- else:
- checkAmount(amount, amountXPath)
- except Exception as e2:
- print('doTheJob : \n')
- print(e2)
- finally:
- doTheJob()
- # The element is avialable or if it is available, lets print it to the console
- # Marco de Boer (c) 2020
- # version 1 : new
- def Available(available, something, tekst):
- if available:
- something = driver.find_element_by_xpath(something).text
- print(tekst + something)
- # Check the amount of requests
- # Marco de Boer (c) 2020
- # version 1 : new
- def checkAmount(amount, amountXPath):
- global i
- if amount == 0:
- print('No requests anymore, lets check if there are new requests')
- driver.refresh()
- sAmount = driver.find_element_by_xpath(amountXPath).text
- sAmount = sAmount.split()[0]
- amount = int(sAmount)
- print('New requests : ' + str(amount))
- print('round : ' + str(i))
- i += 1
- return amount
- # starting point and the inlog
- # Marco de Boer (c) 2020
- # version 1 : new
- def main():
- global driver
- global i
- i = 0
- url = 'https://www.facebook.com/login/?next=https%3A%2F%2Fwww.facebook.com%2Fgroups%2F1237479913012214%2Frequests/'
- driver = webdriver.Firefox()
- driver.get(url)
- driver.implicitly_wait(10)
- driver.find_element(By.ID, 'email').click()
- driver.find_element(By.ID, 'email').send_keys('**********')
- driver.find_element(By.ID, 'pass').send_keys('**********')
- driver.find_element(By.ID, 'pass').send_keys(Keys.ENTER)
- try:
- while True:
- driver.implicitly_wait(10)
- doTheJob()
- except Exception as e:
- print('main :\n ')
- createLogFile('main : \n======================\n')
- print(e + '\n=======================\n')
- # driver.quit()
- finally:
- doTheJob()
- def createLogFile(tekst):
- global f
- if os.path.isfile('requests.txt'):
- f = open('requests.txt', 'a+')
- f.write(tekst + '\n')
- else:
- f = open('requests.txt', 'w+')
- f.write(tekst + '\n')
- # main
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement