模拟登陆小米官网并用某平台自动过验证码(第二版)

终于可以无界面登陆了,验证码使用截图的方式获得

 1 from selenium import webdriver
 2 from selenium.webdriver.chrome.options import Options
 3 from time import sleep
 4 from mytest.selenium.feifeidama import getCode
 5 import time
 6 from PIL import Image
 7 
 8 def get_time_stamp():  # 时间戳
 9     t = time.time()
10     return str(int(t))
11 
12 chrome_options = Options()
13 chrome_options.add_argument('--no-sandbox')
14 chrome_options.add_argument('--headless')
15 
16 driver = webdriver.Chrome(chrome_options=chrome_options)
17 driver.get('https://www.mi.com/index.html')
18 print(driver.title)
19 driver.set_window_size(1400,800)
20 
21 driver.find_element_by_xpath('//a[@href="//order.mi.com/site/login?redirectUrl=https://www.mi.com/index.html"]').click()
22 driver.find_element_by_id('username').send_keys('xxxxxx')
23 sleep(1)
24 driver.find_element_by_id('pwd').send_keys('xxxxxx')
25 sleep(1)
26 driver.find_element_by_id('login-button').click()
27 sleep(1)
28 img = driver.find_element_by_id('captcha-img')
29 
30 img_location = img.location  # 图片坐标点(左上角)
31 img_size = img.size  # 图片的宽高
32 img_width = img_size['width']
33 img_height = img_size['height']
34 img_x = img_location['x']
35 img_y = img_location['y']
36 
37 img_path = '/img/' + get_time_stamp() + '.png'
38 code_path = '/img/' + get_time_stamp() + '_code.png'
39 driver.save_screenshot(img_path)
40 
41 pic = Image.open(img_path)
42 pic = pic.crop((img_x, img_y, img_x + img_width, img_y + img_height))
43 pic.save(code_path)
44 
45 code = getCode(code_path, '30600')  #通过打码平台获取验证码
46 print(code)
47 
48 sleep(1)
49 driver.find_element_by_id('captcha-code').send_keys(code)
50 sleep(1)
51 driver.find_element_by_id('login-button').click()
52 
53 sleep(3)
54 print(driver.title)
55 driver.close()

 

posted @ 2018-12-03 17:26  面向bug编程  阅读(577)  评论(0)    收藏  举报