目录
1.拖动下方滑块完成拼图(单独图片)
2.拖动下方滑块完成拼图(共同图片)
可拖动验证码分为空缺区域为单独的图片和空缺区域与背景图片为一个共同图片,所以实现方式有2种。
1.获取图片验证码滑动区域的距离
def deal_verificationcode():
s = Service("/usr/bin/chromedriver")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument('--headless')
options.add_argument('--no-sandbox')
global driver
driver = webdriver.Chrome(service=s, options=options)
driver.get(url)
driver.find_element(by=By.TAG_NAME, value='textarea').send_keys(key)
button = driver.find_element(by=By.XPATH, value='//*[@id="goodstrackText"]/div[1]/div[2]/div[1]/div/button')
driver.execute_script("$(arguments[0]).click()", button)
sleep(1)
# 裁剪图片距离左侧位置
left_distance1 = driver.find_element(By.ID, value='grap_cut').value_of_css_property('left')
# 空缺区域距离左侧位置
left_distance2 = driver.find_element(By.ID, value='grap_vacant').value_of_css_property('left')
distance = float(left_distance2.split("px")[0]) - float(left_distance1.split("px")[0]) - float(10)
run_slidingblock(distance)
2.距离获取后,拖动验证码
def run_slidingblock(distance):
tracks = get_tracks(distance)
num = 0
for i in tracks:
num += i
if distance - 2 < num and num < distance + 2:
track = tracks
else:
a = distance - num
tracks.append(a)
track = tracks
drag = driver.find_element(by=By.ID, value='swipper-btn')
ActionChains(driver).click_and_hold(drag).perform()
for x in track:
ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
ActionChains(driver).release().perform()
def get_tracks(distance, rate=0.6, t=0.2, v=0):
"""
将distance分割成小段的距离
:param distance: 总距离
:param rate: 加速减速的临界比例
:param a1: 加速度
:param a2: 减速度
:param t: 单位时间
:param t: 初始速度
:return: 小段的距离集合
"""
tracks = []
# 加速减速的临界值
mid = rate * distance
# 当前位移
s = 0
# 循环
while s < distance:
# 初始速度
v0 = v
if s < mid:
a = 40
else:
a = -3
# 计算当前t时间段走的距离
s0 = v0 * t + 0.5 * a * t * t
# 计算当前速度
v = v0 + a * t
# 四舍五入距离,因为像素没有小数
tracks.append(round(s0))
# 计算当前距离
s += s0
return tracks
1.计算背景图片中空缺区域到左侧端点的距离
def deal_verificationcode():
s = Service("/usr/bin/chromedriver")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument('--headless')
options.add_argument('--no-sandbox')
global driver
driver = webdriver.Chrome(service=s, options=options)
driver.get(url)
sleep(1)
driver.switch_to.frame('tcaptcha_iframe')
get_img('cdn1')
get_img('cdn2')
x = FindPic('cdn1.jpg', 'cdn2.jpg')
left_distance1 = driver.find_element(By.ID, value='slideBlock').value_of_css_property('left')
log.info('验证码距离左侧距离为:' + str(left_distance1))
distance = x - float(left_distance1.split("px")[0])
run_slidingblock(distance)
2.距离获取后,拖动验证码
def run_slidingblock(distance):
tracks = get_tracks(distance)
num = 0
for i in tracks:
num += i
if distance - 2 < num and num < distance + 2:
track = tracks
else:
a = distance - num
tracks.append(a)
track = tracks
drag = driver.find_element(by=By.ID, value='swipper-btn')
ActionChains(driver).click_and_hold(drag).perform()
for x in track:
ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
ActionChains(driver).release().perform()
def get_tracks(distance, rate=0.6, t=0.2, v=0):
"""
将distance分割成小段的距离
:param distance: 总距离
:param rate: 加速减速的临界比例
:param a1: 加速度
:param a2: 减速度
:param t: 单位时间
:param t: 初始速度
:return: 小段的距离集合
"""
tracks = []
# 加速减速的临界值
mid = rate * distance
# 当前位移
s = 0
# 循环
while s < distance:
# 初始速度
v0 = v
if s < mid:
a = 40
else:
a = -3
# 计算当前t时间段走的距离
s0 = v0 * t + 0.5 * a * t * t
# 计算当前速度
v = v0 + a * t
# 四舍五入距离,因为像素没有小数
tracks.append(round(s0))
# 计算当前距离
s += s0
return tracks
所谓的人生开挂,不过是厚积薄发! 欢迎评论和转载!