# - *- coding=utf-8-*-
# CodeDemo.py
# PyCharm Slade 2019/7/20
# import selenium,os
from selenium import webdriver
from PIL import Image
def aucthcode(coderddr):"""
Xpath to pass parameter verification code
The full picture of the page is'code.png'Just name this and you are happy
The verification code screenshot is named"aucthcode.png
: param coderddr::return:"""
driver.save_screenshot('code.png')
element = driver.find_element_by_xpath(coderddr) #Get the div position of the verification code
left = element.location['x']+280
top = element.location['y']+81
right = left + element.size['width']+int(13)
bottom = top + element.size['height']+int(2)
img = Image.open('code.png')
imgcod = img.crop((left,top,right,bottom)) #Screenshot according to the length and width of the div
imgcod.save('aucthcode.png')print((left,top,right,bottom))if __name__ =='__main__':
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://user.qunar.com/passport/login.jsp')
driver.find_element_by_css_selector('a.pwd-login').click()aucthcode('//*[@id="captcha"][1]/p[1]')
driver.quit()
# img = Image.open('code.png')
# imgcod = img.crop((1392,393,1490,425)) #Screenshot according to the length and width of the div
# imgcod.save('aucthcodeN.png')
Note: The interception coordinates of the verification code in the code are not necessarily standard. I get the coordinates of the elements as the basic parameters, and then debug in the main to finally get the coordinates I apply
Run effect display code.png
Run the intercepted verification code effect display aucthcode.png
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts