Advertisement
Guest User

Untitled

a guest
Apr 9th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. import json, os
  4.  
  5.  
  6. def alobgUpdate(driver, username_entry, password_entry):
  7.     #load alo.bg login form
  8.     driver.get("https://www.alo.bg/login.php")
  9.  
  10.     #find the input boxes
  11.     username_input = driver.find_element_by_name("username")
  12.     password_input = driver.find_element_by_name("password")
  13.  
  14.     #input the user data and login
  15.     username_input.clear()
  16.     username_input.send_keys(username_entry)
  17.     password_input.clear()
  18.     password_input.send_keys(password_entry)
  19.  
  20.     submit = driver.find_element_by_xpath('//button[@type="submit"]')
  21.     submit.click()
  22.  
  23.     #load the page that lists the ads
  24.     driver.get('http://www.alo.bg/user_online_ads.php')
  25.  
  26.     #get the links for updating all of the ads and click them
  27.     updates = driver.find_elements_by_link_text('Направи първа безплатна')
  28.     for update in updates:
  29.         update.click()
  30.         try:
  31.             alert = driver.switch_to_alert()
  32.             alert.accept()
  33.             print("The ad was update successfully")
  34.         except:
  35.             print("The current ad was already updated today")
  36.  
  37.  
  38. def bazarUpdate(driver, email_entry, password_entry):
  39.     #load the bazar.bg login form
  40.     driver.get("https://bazar.bg/user/login")
  41.  
  42.     #get the input boxes
  43.     email_input = driver.find_element_by_id("mail")
  44.     password_input = driver.find_element_by_id("password")
  45.  
  46.     #input the user data and login
  47.     email_input.clear()
  48.     email_input.send_keys(email_entry)
  49.     password_input.clear()
  50.     password_input.send_keys(password_entry)
  51.  
  52.     submit = driver.find_element_by_name("submitBtn")
  53.     submit.click()
  54.  
  55.     #get the links for updating all of the ads and click them
  56.     updates = driver.find_elements_by_link_text('ОБНОВИ')
  57.     for update in updates:
  58.         try:
  59.             update.click()
  60.             driver.implicitly_wait(1)
  61.             try:
  62.                 close_button = driver.find_element_by_xpath('//*[@title="close"]')
  63.                 close_button.click()
  64.                 print ("The current ad was already updated today")
  65.             except:
  66.                 print ("The current ad was update successfully")
  67.         except:
  68.             print ("Shit happened")
  69.  
  70. if __name__ == '__main__':
  71.     #Get config file path
  72.     config_path = os.path.join(os.path.dirname(__file__), 'config.json')
  73.     #Read the user data
  74.     with open(config_path, 'r') as f:
  75.         config = json.load(f)
  76.     username = config['data']['username']
  77.     password = config['data']['password']
  78.     email = config['data']['email']
  79.  
  80.     #Create an instance of the chrome browser
  81.     driver = webdriver.Chrome()
  82.  
  83.     #Update the alo.bg ads
  84.     alobgUpdate(driver, username, password)
  85.  
  86.     #Update the bazar.bg ads
  87.     bazarUpdate(driver, email, password)
  88.  
  89.     #Close the browser
  90.     driver.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement