Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.19 KB | None | 0 0
  1. from time import localtime, strftime
  2. from time import sleep
  3.  
  4. import sys
  5. from selenium import webdriver
  6. from selenium.common.exceptions import StaleElementReferenceException
  7. # from selenium.webdriver.common.keys import Keys
  8. from selenium.webdriver.chrome.options import Options
  9. from selenium.webdriver.common.action_chains import ActionChains
  10.  
  11. opts = Options()
  12. opts.add_argument(
  13.     "user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
  14.  
  15.  
  16. class WorkChecker:
  17.     def __init__(self, action, user, passw):
  18.         # self.display = Display(visible=0, size=(1920, 1080))
  19.         # self.display.start()
  20.         self.action = action
  21.         self.user = user
  22.         self.passw = passw
  23.         self.driver = webdriver.Chrome()
  24.  
  25.     def __del__(self):
  26.         pass
  27.         # self.display.stop()
  28.         self.driver.close()
  29.  
  30.     def start(self):
  31.         self.driver.get('https://rm.wildberries.ru/#/layout/employee/work-time')
  32.         if self.action == 1:
  33.             print("Доброе утро! Пытаюсь выполнить вход для {}".format(self.user))
  34.         if self.action == 2:
  35.             print("Пора домой. Пытаюсь выполнить выход для {}".format(self.user))
  36.  
  37.         print('Загружаю страницу логина/пароля 6 секунд ...')
  38.         sleep(12)
  39.         user = self.refresh_idfind('globalModalLoginFormUsername')
  40.         passw = self.refresh_idfind('globalModalLoginFormPassword')
  41.         button = self.refresh_idfind('globalModalLoginFormLoginButton')
  42.  
  43.         assert "Торговая точка" in self.driver.title
  44.         user.send_keys(self.user)
  45.         passw.send_keys(self.passw)
  46.         self.click_simple(button)
  47.         print('Перехожу на ввод шк')
  48.         sleep(10)
  49.  
  50.         sk = self.refresh_xfind(
  51.             '//*[@id="main"]/ng-outlet/select-office/page/section/main/page-section/section/main/form/page-section-row/div/div[1]/input')[
  52.             0]
  53.  
  54.         sk.send_keys('OFFC0000100746')
  55.         print('Перехожу на страницу отметок')
  56.         sleep(10)
  57.  
  58.         bt_exit = self.refresh_xfind(
  59.             '//*[@id="main"]/ng-outlet/work-time-registration-component/page/section/main/page-section/section/main/page-section-row[3]/div/div/input[2]')[
  60.             0]
  61.  
  62.         bt_enter = self.refresh_xfind(
  63.             '//*[@id="main"]/ng-outlet/work-time-registration-component/page/section/main/page-section/section/main/page-section-row[3]/div/div/input[1]')[
  64.             0]
  65.  
  66.         if self.action == 1:
  67.             self.click_simple(bt_enter)
  68.             sleep(1)
  69.             data = self.refresh_xfind('//*[@id="main"]/ng-outlet/work-time-registration-component\
  70.            /page/section/main/page-section/section/main/page-section-row[2]/div/div/div[2]/div/label/b')[0].text
  71.             if not self.check_data(data):
  72.                 print('ОШИБКА! Дата не обновилась!')
  73.                 return
  74.         if self.action == 2:
  75.             self.click_simple(bt_exit)
  76.             sleep(1)
  77.             data = self.refresh_xfind('//*[@id="main"]/ng-outlet/work-time-registration-component\
  78.            /page/section/main/page-section/section/main/page-section-row[2]/div/div/div[3]/div/label/b')[0].text
  79.             if not self.check_data(data):
  80.                 print('ОШИБКА! Дата не обновилась!')
  81.                 return
  82.  
  83.         print('Время обновления {}'.format(data))
  84.  
  85.         print('Готово!')
  86.  
  87.     def check_data(self, data):
  88.         '''
  89.        check day
  90.        :param data: str like 06.10.2017 9:47:56
  91.        :return: true if today
  92.        '''
  93.         return data[:10] == strftime("%d.%m.20%y", localtime())
  94.  
  95.     def click_and_hold(self, element):
  96.         ActionChains(self.driver).click_and_hold(element).perform()
  97.  
  98.     def click_simple(self, element):
  99.         ActionChains(self.driver).click(element).perform()
  100.  
  101.     def release(self, element):
  102.         ActionChains(self.driver).release(element).perform()
  103.  
  104.     def refresh_click(self, data):
  105.         for i in range(100):
  106.             try:
  107.                 return self.click_simple(data)
  108.  
  109.             except StaleElementReferenceException:
  110.                 sleep(0.1)
  111.  
  112.     def refresh_xfind(self, path):
  113.         for i in range(10):
  114.             try:
  115.                 return self.driver.find_elements_by_xpath(path)
  116.             except StaleElementReferenceException as ex:
  117.                 sleep(0.5)
  118.                 continue
  119.             except Exception as ex:
  120.                 sleep(0.5)
  121.                 continue
  122.             raise ex
  123.  
  124.     def refresh_idfind(self, id):
  125.         for i in range(10):
  126.             try:
  127.                 return self.driver.find_element_by_id(id)
  128.             except StaleElementReferenceException as ex:
  129.                 sleep(0.5)
  130.             except Exception as ex:
  131.                 sleep(0.5)
  132.         raise ex
  133.  
  134.  
  135. if __name__ == '__main__':
  136.     while True:
  137.         try:
  138.             WorkChecker(action=int(sys.argv[1]), user='', passw='').start()
  139.             break
  140.         except Exception:
  141.             continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement