from selenium import webdriver from selenium. import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time import os from PIL import Image def Element2text(driver,element):#传入driver,和验证码所在element,返回字符串 #print('Element2text(1)') temp = "" try: Photo_time = time.strftime("%Y%m%d_%H%M%S", time.localtime(time.time()))#time库不支持%f毫秒 #print('Element2text(2)') Photo_url = os.getcwd()+"\\"+Photo_time+'.png' time.sleep(0.6)#确保每轮截图能间隔一秒 driver.get_screenshot_as_file(Photo_url) except BaseException as msg: print(msg) return temp
try: #print('Element2text(3)') left = element.location['x'] + 5 #获取验证码最左边位置 top = element.location['y'] #获取验证码最上边位置 right = left + element.size['width'] #获取验证码最右边位置 bottom = top + element.size['height'] #获取验证码最下边位置 except BaseException as msg: print(msg) print('问询验证码坐标信息失败') return temp