selenium模拟12306登录
生活随笔
收集整理的這篇文章主要介紹了
selenium模拟12306登录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver import ActionChains
# 12306模擬登錄過程
# 使用selenium打開登陸界面,對當前selenium打開的界面截圖
# 對當前圖片局部區域(驗證碼照片)進行裁剪,將驗證碼圖片和模擬登錄進行對應
# 使用超級鷹識別驗證碼獲取圖片坐標
class Chaojiying_Client(object):def __init__(self, username, password, soft_id):self.username = usernamepassword = password.encode('utf8')self.password = md5(password).hexdigest()self.soft_id = soft_idself.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):"""im: 圖片字節codetype: 題目類型 參考 http://www.chaojiying.com/price.html"""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('g522627521', 'gxx963214785', '908202') # 用戶中心>>軟件ID 生成一個替換 96001
# im = open('12306.jpg', 'rb').read() # 本地圖片文件路徑 來替換 a.jpg 有時WIN系統須要//
# print(chaojiying.PostPic(im, 9004)['pic_str']) # 1902 驗證碼類型 官方網站>>價格體系 3.4+版 print 后要加()# 使用selemium打開界面
# 加載selenium需要的驅動
driver_path = r'D:\chromedriver2\chromedriver.exe'
options = webdriver.ChromeOptions()
out_path = r'D:\固件下載合集\固件\TPLINK' # 是你想指定的路徑
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': out_path}
options.add_experimental_option('prefs', prefs)
#options.add_argument('window-size=1920x3000') # 指定瀏覽器分辨率
#options.add_argument('blink-settings=imagesEnabled=false') # 不加載圖片
#options.add_argument('--headless') #瀏覽器不提供可視化頁面
driver = webdriver.Chrome(executable_path=driver_path, options=options)
url = 'https://kyfw.12306.cn/otn/login/init'
driver.get(url)
time.sleep(3)# 對當前打開的頁面截圖
driver.save_screenshot('aa.png')# 確定驗證碼圖片對應的左上角和右下角對應的坐標(裁剪的區域)
code_img_ele = driver.find_elements_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')location = code_img_ele.location # 返回的是驗證碼左上角的坐標
size = code_img_ele.size # 驗證碼標簽對應的長和寬
# 左上角和右下角的坐標
rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
# 至此驗證碼圖片區域確定下來
# 實例化image對象
i = Image.open('./aa.png')
code_img_name = './code.png'
# crop根據指定區域進行裁剪
frame = i.crop(rangle)
frame.save(code_img_name)# 將驗證碼碼圖片提交給超級鷹
chaojiying = Chaojiying_Client('g522627521', 'gxx963214785', '908202') # 用戶中心>>軟件ID 生成一個替換 96001
im = open('code.png', 'rb').read() # 本地圖片文件路徑 來替換 a.jpg 有時WIN系統須要//
print(chaojiying.PostPic(im, 9004)['pic_str']) # 1902 驗證碼類型 官方網站>>價格體系 3.4+版 print 后要加()
all_list = [] # 要存儲即將被點擊的點的坐標 [[x1,y1],[x2,y2]]
result = chaojiying.PostPic(im, 9004)['pic_str']
if '|' in result:list_1 = result.split('|')count_1 = len(list_1)for i in range(count_1):xy_list = []x = int(list_1[i].split(',')[0])y = int(list_1[i].split(',')[1])xy_list.append(x)xy_list.append(y)all_list.append(xy_list)
else:x = int(result.split(',')[0])y = int(result.split(',')[1])xy_list = []xy_list.append(x)xy_list.append(y)all_list.append(xy_list)
# 遍歷鏈表,使用動作鏈對每一個列表元素對應的x,y指定的位置進行點擊操作
for l in all_list:x = l[0]y = l[1]ActionChains(driver).move_to_element_with_offset(code_img_ele, x, y).click().perform()time.sleep(0.5)
driver.find_element_by_id('username').send_keys('xxxxxx')
time.sleep(1)
driver.find_element_by_id('password').send_keys('xxxxxx')
time.sleep(1)
driver.find_element_by_id('loginSub').click()
time.sleep(2)
driver.quit()
總結
以上是生活随笔為你收集整理的selenium模拟12306登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [luogu3505][bzoj2088
- 下一篇: 【深入理解 android teleph