标签:turn bdr strong max code style font otto 截图
python3.7
selenium
webdriver
PIL
Image
2.下面demo是截取“去哪儿”官网的验证码
1 # -*- coding=utf-8 -*- 2 # CodeDemo.py 3 # PyCharm Slade 2019/7/20 4 # import selenium,os 5 from selenium import webdriver 6 from PIL import Image 7 8 9 def aucthcode(coderddr): 10 """ 11 传参验证码的Xpath 12 页面全图为‘code.png‘ 命名这个你们开心就好 13 验证码截图名为"aucthcode.png 14 :param coderddr: 15 :return: 16 """ 17 18 driver.save_screenshot(‘code.png‘) 19 element = driver.find_element_by_xpath(coderddr) # 获取验证码的div位置 20 left = element.location[‘x‘] +280 21 top = element.location[‘y‘] +81 22 right = left + element.size[‘width‘] + int(13) 23 bottom = top + element.size[‘height‘] + int(2) 24 img = Image.open(‘code.png‘) 25 imgcod = img.crop((left,top,right,bottom)) # 根据 div的长宽截图 26 imgcod.save(‘aucthcode.png‘) 27 print((left,top,right,bottom)) 28 29 30 if __name__ == ‘__main__‘: 31 driver = webdriver.Chrome() 32 driver.maximize_window() 33 driver.get(‘https://user.qunar.com/passport/login.jsp‘) 34 driver.find_element_by_css_selector(‘a.pwd-login‘).click() 35 aucthcode(‘//*[@id="captcha"][1]/p[1]‘) 36 driver.quit() 37 38 39 # img = Image.open(‘code.png‘) 40 # imgcod = img.crop((1392,393,1490,425)) # 根据 div的长宽截图 41 # imgcod.save(‘aucthcodeN.png‘)
注:代码中的验证码截取坐标,不一定是标准的,我是通过获取元素的坐标为基础参数,然后在main里面进行调试最终得到我适用的坐标
运行效果展示 code.png
运行截取的验证码效果展示 aucthcode.png
标签:turn bdr strong max code style font otto 截图
原文地址:https://www.cnblogs.com/slade-sun/p/11226899.html