selenium-滑动验证码实现
def move_code(self,loc1,loc2,loc3):
'''滑动滑块验证码
loc1 = 滑块图片的元素定位
loc2 = 背景图片的元素定位
loc3 = 滑动按钮的元素定位
脚本位置是在Testcase/Test***/test_**.py
'''
n = 1
while True:
front_img_src = self.get_element_null(loc1)
bg_img_scr = self.get_element_null(loc2)
time.sleep(1)
front_img_src = front_img_src.get_attribute('src')
bg_img_scr = bg_img_scr.get_attribute('src')
os.makedirs('../../outputs/image/', exist_ok=True)
urllib.request.urlretrieve(front_img_src, '../../outputs/image/bkBlock.png')
urllib.request.urlretrieve(bg_img_scr, '../../outputs/image/slideBlock.png')
front = cv2.imread('../../outputs/image/slideBlock.png')
bg = cv2.imread('../../outputs/image/bkBlock.png')
front = cv2.cvtColor(front, cv2.COLOR_BGR2GRAY)
bg = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)
front = front[front.any(1)]
result = cv2.matchTemplate(bg, front, cv2.TM_CCORR_NORMED)
index_max = np.argmax(result)
x, y = np.unravel_index(index_max, result.shape)
drop = self.get_element_null(loc3)
try:
action = ActionChains(self.driver)
action.click_and_hold(drop).perform()
action.drag_and_drop_by_offset(drop, xoffset=y + 10, yoffset=0).perform()
time.sleep(1)
self.get_element_null(loc1).is_displayed()
print('第%s次进行滑动验证码校验' % n, '\n')
n = n + 1
time.sleep(1)
except:
break
|