from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service as EdgeService from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from PIL import Image import time class yidong: def __init__(self, drivePath, money, tel, password, options=Options()): self.chrome_options = options self.chrome_options.add_experimental_option("excludeSwitches",['enable-automation','enable-logging']) self.chrome_options.add_argument("--disable-blink-features") self.chrome_options.add_argument("--disable-blink-features=AutomationControlled") self.chrome_options.add_experimental_option('useAutomationExtension', False) # self.chrome_options.add_argument('--headless') self.service = EdgeService(executable_path=drivePath) self.chrome = webdriver.Edge(service=self.service, options=self.chrome_options) self.chrome.implicitly_wait(10) self.AC = ActionChains(self.chrome) if money % 10 == 0: self.money = money else: print(">>> 金额格式不正确!") exit() self.tel = tel self.password = password def login_page(self): self.chrome.get('https://login.10086.cn/login.html?channelID=12003&backUrl=https://shop.10086.cn/i/?f=rechargecredit') self.AC.move_to_element(self.chrome.find_element(By.XPATH, '//*[@id="J_pc"]')) self.AC.click() self.AC.perform() self.chrome.find_element(By.XPATH, '//*[@id="sms_name"]').send_keys(self.tel) self.chrome.find_element(By.XPATH, '//*[@id="getSMSPwd1"]').click() code = input(">>> [验证码已发送|等待输入]:") self.chrome.find_element(By.XPATH, '//*[@id="sms_pwd_l"]').send_keys(code) self.chrome.find_element(By.XPATH, '//*[ @id="submit_bt"]').click() time.sleep(3) def Cancel_order(self): try: self.chrome.find_element(By.XPATH, '//*[@id="sure-btn"]').click() self.chrome.find_element(By.XPATH, '//*[@id="orderList"]/tbody/tr[1]/td[7]/a[2]').click() jd = self.chrome.find_elements(By.CSS_SELECTOR, 'div.ued-plusdialog')[1] jd.find_element(By.CSS_SELECTOR, 'div.dialog-footer > input:nth-child(1)').click() print(">>> 检测到订单!已取消!") self.chrome.get('https://login.10086.cn/login.html?channelID=12003&backUrl=https://shop.10086.cn/i/?f=rechargecredit') self.chrome.switch_to.window(self.chrome.window_handles[-1]) except: return def invest_page(self): self.Cancel_order() self.chrome.find_element(By.XPATH, '//*[@id="stc-money-input"]').send_keys(self.money) self.chrome.find_element(By.XPATH, '//*[@id="stc-recharge-submit"]').click() time.sleep(3) # 将控制权转为最后打开的标签页面 self.chrome.switch_to.window(self.chrome.window_handles[-1]) self.chrome.find_element(By.XPATH, '//*[@id="payment"]/div/ul/li[3]/label/input').click() self.chrome.find_element(By.XPATH, '//*[@id="payment"]/div/p/input').click() self.chrome.switch_to.window(self.chrome.window_handles[-1]) time.sleep(2) self.chrome.find_element(By.XPATH, '//*[@id="J_qrPayArea"]/div[3]/div[1]').screenshot('payImg.png') img = Image.open('payImg.png') img.show() src_url = self.chrome.current_url while True: if src_url == self.chrome.current_url: time.sleep(5) else: img.close() print(f">>> 充值完成 充值金额 {self.money}") if __name__ == "__main__": Yidong = yidong(drivePath=r'D:\chrome\chromedriver.exe', money=10, tel='18731276617', password='289311') Yidong.login_page() Yidong.invest_page()