from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from PIL import Image
import requests
from hashlib import md5

bro=webdriver.Chrome(r'C:\Program Files (x86)\Google\Chrome\Application\Chromedriver')
bro.get('https://kyfw.12306.cn/otn/resources/login.html')
sleep(5)

log_button=bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a')
log_button.click()
sleep(2)
code_image=bro.find_element_by_xpath('//*[@id="J-loginImg"]')
sleep(2)
location=code_image.location
print(location)
size=code_image.size
print(size)
rangle=(int(location['x']),int(location['y']),(int(location['x'])+int(size['width'])),(int(location['y'])+int(size['height'])))
bro.save_screenshot('aa.png')
i=Image.open('./aa.png')
frame=i.crop(rangle)
frame.save('code.png')

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =password.encode('utf-8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self,im,codetype):
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()

chaojiying = Chaojiying_Client('edanlee95','627195lyd','901679')
im = open('./code.png', 'rb').read()
result=chaojiying.PostPic(im,9004)['pic_str']

all_list=[]
if '|' in result:
    list_1=result.split('|')
    count=len(list_1)
    for i in range(count):
        xy_list = []
        x=list_1[i].split(',')[0]
        y=list_1[i].split(',')[1]
        xy_list.append(x)
        xy_list.append(y)
        all_list.append(xy_list)
else:
    x=result.split(',')[0]
    y=result.split(',')[1]
    xy_list=[]
    xy_list.append(x)
    xy_list.append(y)
    all_list.append(xy_list)

actionchains=ActionChains(bro)

for l in all_list:
    x=int(l[0])
    y=int(l[1])
    actionchains.move_to_element_with_offset(code_image,x,y).click().perform()

bro.find_element_by_xpath('//*[@id="J-userName"]').send_keys('LiYuDong326')
sleep(2)
bro.find_element_by_xpath('//*[@id="J-password"]').send_keys('627195lyd')
sleep(2)
bro.find_element_by_xpath('//*[@id="J-login"]').click()
sleep(2)

bro.quit()

 

posted on 2019-09-27 11:12  edanlee  阅读(279)  评论(0)    收藏  举报