123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- # -*- coding:utf-8 -*-
- # @Time : 2018/5/14 3:58 PM
- # @Author : Swing
- from selenium import webdriver
- from selenium.common.exceptions import TimeoutException
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support.ui import Select
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.common.by import By
- import launch_spider
- import email_util
- import traceback
- browser = webdriver.Chrome()
- # browser.maximize_window()
- timeout = 180
- time_interval = 0.1
- def open_chrome():
- browser.get('https://center.abuyun.com/#/cloud/http-proxy/tunnel/lists')
- def recharge(is_launch_spider=False):
- # 等待网页加载完成
- browser.get('https://center.abuyun.com/#/cloud/http-proxy/tunnel/lists')
- wait_preloader()
- if browser.title == '阿布云 - 服务管理控制台':
- element_xpath = r'//a[text()="动态版"]'
- element = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, element_xpath)))
- element.click()
- # 选中要续费的代理渠道
- checkbox_xpath = r'//tbody/tr/td/div[1]/label/span'
- # WebDriverWait(browser, timeout, 1).until(EC.presence_of_element_located((By.XPATH, checkbox_xpath)))
- checkbox = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, checkbox_xpath)))
- checkbox.click()
- # 点击续费按钮
- try:
- WebDriverWait(browser, timeout, time_interval).until(
- lambda x: x.find_element_by_xpath(r'//tbody/tr/td/div[1]/label/input[@type="checkbox"]').is_selected())
- except TimeoutException:
- email_util.send_email('Abuyun renew timeout', traceback.format_exc())
- except:
- email_util.send_email('Abuyun renew error', traceback.format_exc())
- renew_xpath = r'//div[@class="panel-footer"]/div[@class="row"]/div/button[text()=" 续费"][not(@disabled)]'
- # WebDriverWait(browser, timeout, 1).until(EC.presence_of_element_located((By.XPATH, renew_xpath)))
- renew = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, renew_xpath)))
- renew.click()
- WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, r'//div[@class="modal-dialog modal-mlg"]')))
- # 选择续费周期
- unit_select_xpath = r'//div[@class="input-group unit-input-group"]/select'
- # WebDriverWait(browser, timeout, 1).until(EC.presence_of_element_located((By.XPATH, unit_select_xpath)))
- unit_select = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, unit_select_xpath)))
- select = Select(unit_select)
- select.select_by_visible_text('时')
- number = WebDriverWait(browser, timeout, time_interval).until(lambda x: x.find_element_by_xpath(r'//input[@type="number"]'))
- number.clear()
- number.send_keys('1')
- charge_button = browser.find_element_by_xpath(r'//div[@class="modal-footer"]/button[text()=" 续费"]')
- charge_button.click()
- # 等待充值页面加载成功
- WebDriverWait(browser, timeout, time_interval).until(EC.url_contains('https://center.abuyun.com/#/trade/order/confirm?'))
- WebDriverWait(browser, timeout, time_interval).until(EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "modal")]')))
- wait_preloader()
- balance_xpath = r'//td[span/span="可用余额"]/strong'
- WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, balance_xpath)))
- WebDriverWait(browser, timeout, time_interval).until(EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "modal-backdrop")]')))
- WebDriverWait(browser, timeout, time_interval).until_not(EC.presence_of_element_located((By.XPATH, r'//div[contains(@class, "modal-backdrop")]')))
- if '钱包余额不足, 请先充值或直接使用支付宝支付' in browser.page_source:
- email_util.send_email('Abuyun renew error', 'Lack of balance')
- print('Lack of balance')
- return None
- use_wallet_xpath = r'//table[@class="table table-bordered ng-scope"]/tbody/tr/td[3]/label/span'
- use_wallet = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, use_wallet_xpath)))
- use_wallet.click()
- try:
- balance_string = browser.find_element_by_xpath(balance_xpath).text
- balance = float(balance_string)
- if balance < 40:
- email_util.send_email('Abuyun balance alert', 'Balance: ' + balance_string + ' Please recharge timely!')
- except:
- email_util.send_email('Abuyun balance check error', traceback.format_exc())
- money = WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, r'//span[@class="text-money"]')))
- if money.text == '0.00':
- print('price 0')
- pay = WebDriverWait(browser, timeout, time_interval).until(EC.element_to_be_clickable((By.XPATH, r'//button[text()="马上支付"]')))
- pay.click()
- else:
- email_util.send_email('Abuyun renew error', 'Price not 0')
- return None
- # 等待订单列表加载成功
- WebDriverWait(browser, timeout, time_interval).until(EC.url_contains(r'https://center.abuyun.com/#/trade/order/lists?'))
- wait_preloader()
- WebDriverWait(browser, timeout, time_interval).until(
- EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "modal")]')))
- WebDriverWait(browser, timeout, time_interval).until(EC.visibility_of_element_located((By.XPATH, r'//div[contains(@class, "sweet-alert")]')))
- confirm_button = WebDriverWait(browser, timeout, time_interval).until(
- EC.element_to_be_clickable((By.XPATH, r'//button[@class="confirm"]')))
- if '已从钱包扣款,订单支付完成' in browser.page_source:
- print('Renew completed')
- if is_launch_spider:
- launch_spider.start_spider()
- confirm_button.click()
- else:
- email_util.send_email('Abuyun renew error', 'Abuyun renew error')
- print('Renew failed')
- else:
- email_util.send_email('Abuyun Login failed', 'Abuyun Login failed')
- print('Login failed')
- def wait_preloader():
- WebDriverWait(browser, timeout, time_interval).until(
- EC.invisibility_of_element_located((By.XPATH, r'//div[contains(@class, "preloader")]')))
|