使用selenium操作滑块的时候使用了随机生成的轨迹但是会导致滑块拉过

一开始我正常写,是可以滑到图片缺口位置,但是之后会提示网络波动(其实就被识别是机器人拖动的滑块了)这是一开始的代码

from selenium import webdriver
import requests
import cv2.cv2 as cv2
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ChromeOptions  # 这个包用来规避被检测的风险
import time  # 延迟
from selenium.webdriver import ActionChains  # 动作链

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
driver_path = r'C:\Users\哥斯拉\AppData\Local\Google\Chrome\Application\chromedriver.exe'  # 定义好路径

driver = webdriver.Chrome(executable_path=driver_path, options=option)  # 初始化路径+规避检测


def SF():
    driver.get('https://www.sf-express.com/cn/sc/dynamic_function/waybill/')  # SF1300477886754
    time.sleep(6)
    kk = driver.find_element_by_xpath('//input[@class="token-input"]').send_keys('SF1300477886754') #输入框
    time.sleep(1)
    register = driver.find_element_by_xpath('//*[@id="queryBill"]').click()  #查询按钮
    # 切换到滑块的网址去操作元素
    time.sleep(4)
    driver.switch_to_frame("tcaptcha_popup")
    while True:
        # 找到背景图
        ele_bj = driver.find_element_by_xpath('//img[@id="slideBkg"]')
        # 找到滑块图片
        ele_hk = driver.find_element_by_xpath('//*[@id="slideBlock"]')
        # 获取背景图下载地址
        src_bj = ele_bj.get_attribute('src')
        # 获取滑块图片下载地址
        src_hk = ele_hk.get_attribute('src')
        #用resqust下载图片
        content=requests.get(src_bj).content  #下载背景
        f=open('bj.jpg',mode='wb')
        f.write(content)
        f.close()
        time.sleep(1)
        content1 = requests.get(src_hk).content #下载滑块
        f = open('hk.jpg', mode='wb')
        f.write(content1)
        f.close()
        GG()
        x=GG()
        #计算缩放比
        x=int(x*340/680)-10
        # get_track(x)
        #使用selenium滑动
        action=ActionChains(driver)
        #按住滑块元素
        action.click_and_hold(ele_hk).perform()
        #滑动
        action.move_by_offset(x,0).perform()
        #松开鼠标
        action.release(ele_hk).perform()
        try:
            time.sleep(3)
            refresh=driver.find_element_by_xpath('//*[@id="reload"]').click()  #刷新按钮
        except Exception as e:
            break
def GG():
    #解析X的距离
    #读取图片的RGB
    bj_rgb=cv2.imread('bj.jpg')
    #灰度处理
    bj_gray=cv2.cvtColor(bj_rgb,cv2.COLOR_BGR2GRAY)
    #读取滑块的RGB
    hk_rgb=cv2.imread('hk.jpg',0)
    #匹配滑块在背景图的位置
    res=cv2.matchTemplate(bj_gray,hk_rgb,cv2.TM_CCOEFF_NORMED)
    #获取位置
    location=cv2.minMaxLoc(res)
    print(location[2][0])
    return location[2][0]

if __name__ == '__main__':
    SF()

后面我弄了一个随机轨迹,但是吧会导致拉动滑块的元素一直往右边拉,我手动给它拉回来它还是一直往前,这是什么情况造成的啊,麻烦帮忙看看代码该怎么改

img

这是生成轨迹之后的代码


from selenium import webdriver
import requests
import cv2.cv2 as cv2
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ChromeOptions  # 这个包用来规避被检测的风险
import time  # 延迟
from selenium.webdriver import ActionChains  # 动作链

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
driver_path = r'C:\Users\哥斯拉\AppData\Local\Google\Chrome\Application\chromedriver.exe'  # 定义好路径

driver = webdriver.Chrome(executable_path=driver_path, options=option)  # 初始化路径+规避检测


def get_tracks(distance):
    """
    v = v0+at
    x = v0t+1/2at**2
    """
    # 定义存放运动轨迹的列表
    tracks = []
    # 定义初速度
    v = 0
    # 定义单位时间
    t = 0.5
    # 定义匀加速运动和匀减速运动的分界线
    mid = distance * 4/5
    # 定义当前位移
    current = 0
    # 为了一直移动,定义循环
    while current < distance:
        if mid > current:
            a = 2
        else:
            a = -3
        v0 = v
        # 计算位移
        x = v0 * t + 1/2*a*t**2
        # 计算滑块当前位移
        current += x
        # 计算末速度
        v = v0+a*t
        tracks.append(round(x))
    return tracks
def SF():
    driver.get('https://www.sf-express.com/cn/sc/dynamic_function/waybill/')  # SF1300477886754
    time.sleep(6)
    kk = driver.find_element_by_xpath('//input[@class="token-input"]').send_keys('SF1300477886754') #输入框
    time.sleep(1)
    register = driver.find_element_by_xpath('//*[@id="queryBill"]').click()  #查询按钮
    # 切换到滑块的网址去操作元素
    time.sleep(4)
    driver.switch_to_frame("tcaptcha_popup")
    while True:
        # 找到背景图
        ele_bj = driver.find_element_by_xpath('//img[@id="slideBkg"]')
        # 找到滑块图片
        ele_hk = driver.find_element_by_xpath('//*[@id="slideBlock"]')
        # 获取背景图下载地址
        src_bj = ele_bj.get_attribute('src')
        # 获取滑块图片下载地址
        src_hk = ele_hk.get_attribute('src')
        #用resqust下载图片
        content=requests.get(src_bj).content  #下载背景
        f=open('bj.jpg',mode='wb')
        f.write(content)
        f.close()
        time.sleep(1)
        content1 = requests.get(src_hk).content #下载滑块
        f = open('hk.jpg', mode='wb')
        f.write(content1)
        f.close()
        GG()
        i=GG()
        #计算缩放比
        i=int(i*340/680)-12
        print(i)
        tracks = get_tracks(i)
        #使用selenium滑动
        action=ActionChains(driver)
        #按住滑块元素
        action.click_and_hold(ele_hk).perform()
        #使用滑动轨迹滑动
        for track in tracks:
            action.move_by_offset(xoffset=track,yoffset=0).perform()
        #松开鼠标
        time.sleep(1)
        action.release(ele_hk).perform()
        try:
            time.sleep(4)
            refresh=driver.find_element_by_xpath('//*[@id="reload"]').click()  #刷新按钮
        except Exception as e:
            break

def GG():
    #解析X的距离
    #读取图片的RGB
    bj_rgb=cv2.imread('bj.jpg')
    #灰度处理
    bj_gray=cv2.cvtColor(bj_rgb,cv2.COLOR_BGR2GRAY)
    #读取滑块的RGB
    hk_rgb=cv2.imread('hk.jpg',0)
    #匹配滑块在背景图的位置
    res=cv2.matchTemplate(bj_gray,hk_rgb,cv2.TM_CCOEFF_NORMED)
    #获取位置
    location=cv2.minMaxLoc(res)
    print(location[2][0])
    return location[2][0]

if __name__ == '__main__':
    SF()

同学你好,直接定义匀速运动不行吗,匀加速和匀减速直线都有惯性,不好掌控;
你可以这样设计,你的鼠标位置在左边,就匀速运动到左边,鼠标位置在右边就匀速运动到右边,然后响应时间比如说0.01s,这样就能随时滑动了