Pole test verification Canvas

Source: https: //blog.csdn.net/qq_28654919/article/details/89923687

Do not use for business use Oh, only to learn, or peril oh
there are two articles on the pole test to verify the slider to break, here to share with you:
https://www.jianshu.com/p/c8df1194b514
HTTPS: / /www.jianshu.com/p/f12679a63b8d
these two articles for the different sliders, there are different methods to find gaps and sliding, gains a lot.
This blog is based on the first article written, find the gap and a method of sliding is exactly the same. The only difference is that the way to save the image, the first article there is a picture link address, and the picture is out of order. I am here canvas. So here only the first half, the second half looking for gaps and sliding distance, please refer to the original.
Text
crawl URL: https: //www.binance.co/login.html

First of all, you can see the picture is presented in the form of canvas, it is not possible to download to your local.
Secondly, we have to find a gap in the picture and complete the picture.
Carefully observe the class name, you can obviously find, geetest_canvas_bg there is a gap of background, geetest_canvas_slice is that small gap, geetest_canvas_fullbg complete picture, I have noticed a green frame it, you just put this to delete it you can see the complete picture, css it only hides the fact that it is there.
Reference to the first way, so I need to download here, there are gaps in the background and complete the picture, the specific method is as follows
DEF save_img (Self, img_name, class_name):
# img_name is to save the image name, class_name is needed to save the the canvas className
getImgJS = 'return document.getElementsByClassName ( "' + + class_name '") [0] .toDataURL ( "Image / PNG");'
IMG = self.driver.execute_script (getImgJS)
base64_data_img IMG = [img.find ( ',') +. 1:]
IMAGE_BASE = base64.b64decode (base64_data_img)
File = Open (img_name, 'WB')
a file.write (IMAGE_BASE)
File.

Other changes not much, this distance may be adjusted according to specific circumstances, in which method start_move, I point raised distance, becomes: distance + = 25.
The following is the full code
Import Random
Import Time, Re
from the Selenium Import webdriver
from selenium.common.exceptions Import TimeoutException
from selenium.webdriver.common.by By Import
from selenium.webdriver.support.wait Import WebDriverWait
from selenium.webdriver.support Import expected_conditions EC AS
from selenium.webdriver.common.action_chains Import ActionChains
from PIL Import Image
Import Base64

'' '
Pole test verification slider version 3.0
' ''

class Binance(object):
def __init__(self):
chrome_option = webdriver.ChromeOptions()
chrome_option.add_experimental_option('excludeSwitches', ['enable-automation'])
self.driver = webdriver.Chrome(chrome_options=chrome_option)
self.driver.set_window_size(1440, 900)

def visit_index(self):
# 输入邮箱和密码
self.driver.get("https://www.binance.co/login.html")
email = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, 'email')))
email.clear()
email.send_keys("[email protected]")
pwd = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, 'pwd')))
pwd.clear()
pwd.send_keys("xxxxxxxxx")

# Click logon, pop-up slider codes
login_btn = WebDriverWait (self.driver, 10, 0.5) .until (EC.element_to_be_clickable ((By.ID, 'Login-BTN')))
login_btn.click ()
WebDriverWait (Self. driver, 10, 0.5) .until ( EC.presence_of_element_located ((By.CLASS_NAME, 'geetest_canvas_fullbg')))

# Drag into the simulation process
self.analog_drag ()

def analog_drag(self):

# Refresh pole test picture
Element = self.driver.find_element_by_xpath ( '// A [@ class = "geetest_refresh_1"]')
element.click ()
the time.sleep (1)

# Save two pictures
self.save_img ( 'full.jpg', 'geetest_canvas_fullbg')
self.save_img ( 'cut.jpg', 'geetest_canvas_bg')
full_image = Image.open ( 'full.jpg')
cut_image = Image.open ( 'cut.jpg')

The two images to calculate the distance #
distance = self.get_offset_distance (cut_image, full_image)

# Start moving
self.start_move (distance)

# 如果出现error
try:
WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_element_located((By.XPATH, '//div[@class="geetest_slider geetest_error"]')))
print("验证失败")
return
except TimeoutException as e:
pass

# Verification is successful is determined whether
the try:
WebDriverWait (self.driver, 10, 0.5) .until (
EC.presence_of_element_located ((By.XPATH, '// div [@ class = "geetest_slider geetest_success"]')))
the except a TimeoutException:
Print ( "Again Times")
self.analog_drag ()
the else:
Print ( "verification success")

def save_img(self, img_name, class_name):
getImgJS = 'return document.getElementsByClassName("' + class_name + '")[0].toDataURL("image/png");'
img = self.driver.execute_script(getImgJS)
base64_data_img = img[img.find(',') + 1:]
image_base = base64.b64decode(base64_data_img)
file = open(img_name, 'wb')
file.write(image_base)
file.close()

# 判断颜色是否相近
def is_similar_color(self, x_pixel, y_pixel):
for i, pixel in enumerate(x_pixel):
if abs(y_pixel[i] - pixel) > 50:
return False
return True

# Calculated distance
DEF get_offset_distance (Self, cut_image, full_image):
for X in Range (cut_image.width):
for Y in Range (cut_image.height):
CPX cut_image.getpixel = ((X, Y))
FPX = full_image.getpixel ((the X-, the y-))
IF not self.is_similar_color (CPX, FPX):
img = cut_image.crop ((the X-, the y-, the X-+ 50, the y-+ 40))
# save the calculated position of the picture look, see if it is cutaway portion
img.save ( "1.png")
return the X-

# 开始移动
def start_move(self, distance):
element = self.driver.find_element_by_xpath('//div[@class="geetest_slider_button"]')

# This is debugging a mobile, the position is not calculated correctly hundred percent, with a little offset
Distance - = element.size.get ( 'width') / 2
Distance = + 25

Pressing the left mouse button #
ActionChains (self.driver) .click_and_hold (Element) .perform ()
the time.sleep (0.5)
the while Distance> 0:
IF Distance> 10:
# If the distance is greater than 10, let him move faster
span the random.randint = (. 5,. 8)
the else:
# approaching the gap, moves slower
span the random.randint = (2,. 3)
ActionChains (self.driver) .move_by_offset (span, 0) .perform ()
Distance - span =
the time.sleep (the random.randint (10, 50) / 100)

ActionChains(self.driver).move_by_offset(distance, 1).perform()
ActionChains(self.driver).release(on_element=element).perform()

if __name__ == "__main__":
b = Binance()
b.visit_index()

Guess you like

Origin www.cnblogs.com/zzllovehappy/p/11112690.html