abuyun_renew.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding:utf-8 -*-
  2. # @Time : 2018/5/14 3:58 PM
  3. # @Author : Swing
  4. from selenium import webdriver
  5. from selenium.common.exceptions import TimeoutException
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.webdriver.support.ui import Select
  8. from selenium.webdriver.support import expected_conditions as EC
  9. from selenium.webdriver.common.by import By
  10. import launch_spider
  11. import email_util
  12. import traceback
  13. browser = webdriver.Chrome()
  14. # browser.maximize_window()
  15. timeout = 180
  16. time_interval = 0.1
  17. def open_chrome():
  18. browser.get('https://center.abuyun.com/#/cloud/http-proxy/tunnel/lists')
  19. def recharge(is_launch_spider=False):
  20. # 等待网页加载完成
  21. browser.get('https://center.abuyun.com/#/cloud/http-proxy/tunnel/lists')
  22. wait_preloader()
  23. if browser.title == '阿布云 - 服务管理控制台':
  24. element_xpath = r'//a[text()="动态版"]'
  25. element = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, element_xpath)))
  26. element.click()
  27. # 选中要续费的代理渠道
  28. checkbox_xpath = r'//tbody/tr/td/div[1]/label/span'
  29. # WebDriverWait(browser, timeout, 1).until(EC.presence_of_element_located((By.XPATH, checkbox_xpath)))
  30. checkbox = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, checkbox_xpath)))
  31. checkbox.click()
  32. # 点击续费按钮
  33. try:
  34. WebDriverWait(browser, timeout, time_interval).until(
  35. lambda x: x.find_element_by_xpath(r'//tbody/tr/td/div[1]/label/input[@type="checkbox"]').is_selected())
  36. except TimeoutException:
  37. email_util.send_email('Abuyun renew timeout', traceback.format_exc())
  38. except:
  39. email_util.send_email('Abuyun renew error', traceback.format_exc())
  40. renew_xpath = r'//div[@class="panel-footer"]/div[@class="row"]/div/button[text()=" 续费"][not(@disabled)]'
  41. # WebDriverWait(browser, timeout, 1).until(EC.presence_of_element_located((By.XPATH, renew_xpath)))
  42. renew = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, renew_xpath)))
  43. renew.click()
  44. WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, r'//div[@class="modal-dialog modal-mlg"]')))
  45. # 选择续费周期
  46. unit_select_xpath = r'//div[@class="input-group unit-input-group"]/select'
  47. # WebDriverWait(browser, timeout, 1).until(EC.presence_of_element_located((By.XPATH, unit_select_xpath)))
  48. unit_select = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, unit_select_xpath)))
  49. select = Select(unit_select)
  50. select.select_by_visible_text('时')
  51. number = WebDriverWait(browser, timeout, time_interval).until(lambda x: x.find_element_by_xpath(r'//input[@type="number"]'))
  52. number.clear()
  53. number.send_keys('1')
  54. charge_button = browser.find_element_by_xpath(r'//div[@class="modal-footer"]/button[text()=" 续费"]')
  55. charge_button.click()
  56. # 等待充值页面加载成功
  57. WebDriverWait(browser, timeout, time_interval).until(EC.url_contains('https://center.abuyun.com/#/trade/order/confirm?'))
  58. WebDriverWait(browser, timeout, time_interval).until(EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "modal")]')))
  59. wait_preloader()
  60. balance_xpath = r'//td[span/span="可用余额"]/strong'
  61. WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, balance_xpath)))
  62. WebDriverWait(browser, timeout, time_interval).until(EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "modal-backdrop")]')))
  63. WebDriverWait(browser, timeout, time_interval).until_not(EC.presence_of_element_located((By.XPATH, r'//div[contains(@class, "modal-backdrop")]')))
  64. if '钱包余额不足, 请先充值或直接使用支付宝支付' in browser.page_source:
  65. email_util.send_email('Abuyun renew error', 'Lack of balance')
  66. print('Lack of balance')
  67. return None
  68. use_wallet_xpath = r'//table[@class="table table-bordered ng-scope"]/tbody/tr/td[3]/label/span'
  69. use_wallet = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, use_wallet_xpath)))
  70. use_wallet.click()
  71. try:
  72. balance_string = browser.find_element_by_xpath(balance_xpath).text
  73. balance = float(balance_string)
  74. if balance < 40:
  75. email_util.send_email('Abuyun balance alert', 'Balance: ' + balance_string + ' Please recharge timely!')
  76. except:
  77. email_util.send_email('Abuyun balance check error', traceback.format_exc())
  78. money = WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, r'//span[@class="text-money"]')))
  79. if money.text == '0.00':
  80. print('price 0')
  81. pay = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, r'//button[text()="马上支付"]')))
  82. pay.click()
  83. else:
  84. email_util.send_email('Abuyun renew error', 'Price not 0')
  85. return None
  86. # 等待订单列表加载成功
  87. WebDriverWait(browser, timeout, time_interval).until(EC.url_contains(r'https://center.abuyun.com/#/trade/order/lists?'))
  88. wait_preloader()
  89. WebDriverWait(browser, timeout, time_interval).until(
  90. EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "modal")]')))
  91. WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, r'//div[contains(@class, "sweet-alert")]')))
  92. confirm_button = WebDriverWait(browser, timeout, time_interval).until(
  93. EC.element_to_be_clickable((By.XPATH, r'//button[@class="confirm"]')))
  94. if '已从钱包扣款,订单支付完成' in browser.page_source:
  95. print('Renew completed')
  96. if is_launch_spider:
  97. launch_spider.start_spider()
  98. confirm_button.click()
  99. else:
  100. email_util.send_email('Abuyun renew error', 'Abuyun renew error')
  101. print('Renew failed')
  102. else:
  103. email_util.send_email('Abuyun Login failed', 'Abuyun Login failed')
  104. print('Login failed')
  105. def wait_preloader():
  106. WebDriverWait(browser, timeout, time_interval).until(
  107. EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "preloader")]')))