I pure wild Test ape One, did a year web function test, test a year set-top box, or want to do back to the old bank, self-study for a period of time selenium, learning process, encountered the need to enter the Web page verification code problems, online access to information, According to gourd painting Scoop (level Limited) a section of the landing site to hang the script (pro-test pass), now share with you as follows:
Big God don't squirt
From selenium import Webdriver
From Selenium.webdriver.common.action_chains import Actionchains
Import time
From PIL import Image
From PIL import imageenhance
Import Pytesseract
b = Webdriver. Firefox ()
B.get ("http://www.ouchn.cn/")
Time.sleep (5)
Ele = b.find_element_by_css_selector\
("HTML body div#ding Div.din div.di2 a.loginbtn")
#print (Type (ele))
Ele.click () #点击登录
Time.sleep (8)
#print (B.window_handles)
B.switch_to_window (B.window_handles[1])
#print (B.current_url)
#b. Get (B.current_url)
B.save_screenshot (' e://aa.png ') #截取当前网页, the page has the verification code we need
Time.sleep (6)
ImgElement = b.find_element_by_css_selector ("HTML body form div#box div.box DIV.KK table tbody TR TD label IMG") #定位验证码
Location1 = Imgelement.location_once_scrolled_into_view
Print (Location1)
coderange= (int (location1[' x ')), int (location1[' y ']+100), int ((location1[' x ']-30) +130), int ((location1[' y ']+130)) #写成我们需要截取的位置坐标
I=image.open (r "E:\aa.png") #打开
Frame4=i.crop (Coderange) #使用Image的crop函数 to intercept the area we need again
Frame4.save (r "E:\frame4.png")
I2=image.open (r "E:\frame4.png")
Imgry = I2.convert (' L ') #图像加强, binary, nine different modes in PIL. The 1,l,p,rgb,rgba,cmyk,ycbcr,i,f are respectively. L is a grayscale image
Sharpness =imageenhance.contrast (imgry) #对比度增强
i3 = sharpness.enhance (3.0) #3.0 The saturation of the image
I3.save ("E:\\image_code.png")
I4 = Image.open ("E:\\image_code.png")
Text = pytesseract.image_to_string (i4). Strip () #使用image_to_string识别验证码
Print (text) #调试用 to see if the verification code identification was successful
B.FIND_ELEMENT_BY_ID ("username"). Clear ()
B.FIND_ELEMENT_BY_ID ("username"). Send_keys ("Mosaic here")
b.find_element_by_id ("password"). Send_keys ("Mosaic here")
B.find_element_by_css_selector ("HTML body form Div#box Div.box DIV.KKTable tbody tr TD label ")
"Input#checkcode.biaodan2"). Send_keys (text)
Time.sleep (5)
B.find_element_by_css_selector ("HTML body form div#box div.box DIV.KK table tbody tr TD Table Tbody TR TD label INPUT#BTNL Ogin.anniu "). Click ()
#b. Quit ()
On the way to see the great God with location method can locate the image element position, with size method can locate the length and width size, but I found in the use of these two methods are not, direct error (I use the Python3, may be related to this). Later accidentally found location of the elder brother Location_once_scrolled_into_view, tried, wo plug, incredibly able to locate the coordinates. But the size method still does not work, and later through the review element discovery can also find the length and width of the image element. Finally, a few tests have been done to accurately intercept the verification code image by line recognition.
Python Selenium automated test image recognition 1