from selenium import webdriver
from PIL import Image
from time import sleep
login_12306 = webdriver.Chrome(executable_path=r‘D:\python学习\其他\chromedriver.exe‘)
login_12306.maximize_window() # 设置浏览器全屏
login_12306.get(‘https://kyfw.12306.cn/otn/resources/login.html‘) # 用户发起请求
sleep(0.5)
password_login = login_12306.find_element_by_xpath(‘/html/body/div[2]/div[2]/ul/li[2]/a‘).click()
sleep(0.5)
# 保存网页截图
login_12306.save_screenshot(‘12306_home.png‘)
# 定位验证码图片标签
image_code = login_12306.find_element_by_id(‘J-loginImg‘)
# 验证码图片左上角坐标、验证码图片长宽
location = image_code.location # {‘x‘: 851, ‘y‘: 193}
size = image_code.size # {‘height‘: 36, ‘width‘: 100}
# 电脑分辨率问题,如果 分辨率太大,要乘以相应的倍数
left = location[‘x‘]*1.5
top = location[‘y‘]*1.5
right = left + size[‘width‘]*1.5
bottom = top + size[‘height‘]*1.5
range = (left, top, right, bottom)
i = Image.open(‘12306_home.png‘)
frame = i.crop(range)
frame.save(‘12306_code.png‘)
原文地址:https://www.cnblogs.com/zjj999/p/12612312.html