# coding:utf-8 import location from selenium import webdriver from PIL import Image import requests from hashlib import md5 from selenium.webdriver.common.action_chains import ActionChains
bro = webdriver.Chrome(executable_path='./chromedriver.exe')
bro.get('https://kyfw.12306.cn/otn/resources/login.html')
w = bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a')
w.click()
# 将当前页面进行截图并保存 save_screenshot
bro.save_screenshot('a.png')
# 确定验证码图片对应的左上角和右下角的坐标(确定截图区域)
code_img_ele = bro.find_elements_by_id('J-loginImg')
location = code_img_ele.location #验证码图片左上角的标签x,y
print('location',location)
size = code_img_ele.size #验证码标签对应的长和宽
print('size',size)
# 左上角和右下角坐标
rangle = (
location['x'],location['y'],location['x']+size['width'],location['y']+size['hight']
)
# 至此确定了验证码区域
i = Image.open('a.png')
code_img_name = 'code,png'
# crop根据指定区域进行 图片裁剪
frame = i.crop(rangle)
frame.save(code_img_name)
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/pythonProject/爬虫学习/第七章:动态加载数据处理/07.基于selenium实现12306模拟登录.py", line 62, in <module>
location = code_img_ele.location #验证码图片左上角的标签x,y
AttributeError: 'list' object has no attribute 'location'