|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.webdriver.support.ui import WebDriverWait |
| 3 | +from selenium.webdriver.support import expected_conditions as EC |
| 4 | +from selenium.webdriver.common.by import By |
| 5 | +import time,base64 |
| 6 | +import chaojiying |
| 7 | +from selenium.webdriver import ActionChains |
| 8 | + |
| 9 | +class Ticket(object): |
| 10 | + |
| 11 | + def __init__(self, username, password): |
| 12 | + self.username = username |
| 13 | + self.password = password |
| 14 | + self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html' |
| 15 | + |
| 16 | + |
| 17 | + def findElement(self, type, id): |
| 18 | + # 查找元素 |
| 19 | + return EC.visibility_of_element_located((type, id)) |
| 20 | + |
| 21 | + def login(self): |
| 22 | + self.driver = webdriver.Chrome(executable_path='D:\chromedriver.exe') |
| 23 | + |
| 24 | + with open('D:\stealth.min.js') as f: |
| 25 | + stealth = f.read() |
| 26 | + self.driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {"source": stealth}) |
| 27 | + |
| 28 | + self.wait = WebDriverWait(self.driver, 10, 0.1) |
| 29 | + self.driver.get(self.login_url) |
| 30 | + |
| 31 | + self.wait.until(self.findElement(By.LINK_TEXT,'账号登录')).click() |
| 32 | + |
| 33 | + self.wait.until(self.findElement(By.ID, 'J-userName')).send_keys(self.username) |
| 34 | + |
| 35 | + self.wait.until(self.findElement(By.ID, 'J-password')).send_keys(self.password) |
| 36 | + |
| 37 | + success_flag = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'lgcode-success'))).get_attribute('style') |
| 38 | + while success_flag == 'display: none;': |
| 39 | + img = self.wait.until(EC.visibility_of_element_located((By.ID, 'J-loginImg'))) |
| 40 | + |
| 41 | + base64Img = img.get_attribute('src') |
| 42 | + base64Img = base64Img.replace('data:image/jpg;base64,', '') |
| 43 | + imgdata=base64.urlsafe_b64decode(base64Img) |
| 44 | + file=open('1.jpg','wb') |
| 45 | + file.write(imgdata) |
| 46 | + file.close() |
| 47 | + |
| 48 | + cj = chaojiying.Chaojiying_Client('用户名', '密码', '软件ID') |
| 49 | + im = open('1.jpg', 'rb').read() |
| 50 | + cjy_result = cj.PostPic(im, 9004) |
| 51 | + print(cjy_result) |
| 52 | + x_y = cjy_result['pic_str'] |
| 53 | + pic_id = cjy_result['pic_id'] |
| 54 | + |
| 55 | + all_list = [] |
| 56 | + for i in x_y.split('|'): |
| 57 | + all_list.append([int(i.split(',')[0]), int(i.split(',')[1])]) |
| 58 | + |
| 59 | + for rangle in all_list: |
| 60 | + ActionChains(self.driver).move_to_element_with_offset(img, rangle[0], rangle[1]).click().perform() |
| 61 | + |
| 62 | + self.wait.until(self.findElement(By.ID, 'J-login')).click() |
| 63 | + success_flag = self.driver.find_element_by_class_name('lgcode-success').get_attribute('style') |
| 64 | + |
| 65 | + if success_flag == 'display: none;': |
| 66 | + cj.ReportError(pic_id) |
| 67 | + |
| 68 | + nc_1_n1z = self.wait.until(self.findElement(By.ID, 'nc_1_n1z')) |
| 69 | + tracks = [6,16,31,52,72,52,62,50] |
| 70 | + |
| 71 | + action = ActionChains(self.driver) |
| 72 | + |
| 73 | + action.click_and_hold(nc_1_n1z).perform() |
| 74 | + for track in tracks: |
| 75 | + action.move_by_offset(track, 0) |
| 76 | + time.sleep(0.5) |
| 77 | + action.release().perform() |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + username = 'xxxx' |
| 81 | + password = 'xxxdddxx' |
| 82 | + |
| 83 | + ticket = Ticket(username, password) |
| 84 | + ticket.login() |
0 commit comments