思路:

1 找到距离 
2 生成滑动轨迹  
3 模拟事件
两张图片 一个缺口图片 一个带缺口的完整图

1.PNG
2.PNG
参考博客

向右滑到底

3.png
1 首先获取滑动的距离 末端 – 小图标 中心点 适当多点无所谓
2 生成轨迹

#生成直线轨迹
  def get_tracks_2( distance, seconds, ease_func):
        tracks = [0]
        offsets = [0]
        for t in np.arange(0.0, seconds, 0.1):
            ease = ease_func
            offset = round(ease(t / seconds) * distance)
            tracks.append(offset - offsets[-1])
            offsets.append(offset)
        return tracks

3 最后一步 模拟事件 滑到位置


 def move_to_gap(self, track):
        self._driver.web_driver_wait(2, self._driver.XPATH, \'xpath 路径\')
        slider = self._driver.find_element_by_xpath(\'xpath 路径\')
        action = ActionChains(self._driver)
        action.click_and_hold(slider)
        while track:
            x = track.pop(0)
            y = x % 2 * random.choice([4, 5, 2])
            action.move_by_offset(xoffset=x, yoffset=y)
        time.sleep(0.5)  # 这里不加延时会导致滑块失败
        action.release().perform()
另一种情况 篮球入框

图片是一个小篮球 和 一个篮球框
4.png
1 计算篮球和框的坐标

  self._driver.web_driver_wait(1, self._driver.XPATH, \'//div[@class="rds-drag-basket"]\')
                    basket_style = self._driver.find_element_by_xpath(\'xpath 路径\').get_attribute("style")
                    ball_style = self._driver.find_element_by_xpath(\'xpath 路径\').get_attribute(\'style\')
                    basket_left = re.findall(r"left: (.+?)px", basket_style)[0]
                    basket_top = re.findall(r"top: (.+?)px", basket_style)[0]
                    ball_left = re.findall(r"left: (.+?)px", ball_style)[0]
                    ball_top = re.findall(r"top: (.+?)px", ball_style)[0]
                    x = int(basket_left.split(\'.\')[0]) - int(ball_left.split(\'.\')[0]) + 23
                    y = int(basket_top.split(\'.\')[0]) - int(ball_top.split(\'.\')[0]) + 23
                    self.move_to_basket(x, y)

2 生成轨迹 并移动

 def move_to_basket(self, x, y):
        slider = self._driver.find_element_by_xpath(\'xpath 路径\')
        action = ActionChains(self._driver)
        action.click_and_hold(slider)
        track1 = self.get_tracks_2(x + random.choice([-1, -2, -3, -4, -5, 1, 2, 3, 4, 5]), random.randint(2, 4), self.ease_out_quart)
        track2 = self.get_tracks_2(y + random.choice([-1, -2, -3, -4, -5, 1, 2, 3, 4, 5]), random.randint(2, 4), self.ease_out_quart)
        while track1 or track2:
            try:
                x = track1.pop(0)
            except:
                x = 0
            try:
                y = track2.pop(0)
            except:
                y = 0
            action.move_by_offset(xoffset=x, yoffset=y)
        time.sleep(0.5)  # 这里不加延时会导致滑块失败
        action.release().perform()

最后一种 三张图片 完整的 完整带缺口的 缺口图片

1 比较两张图片 然后灰度值 算出缺口位置

 def compute_gap(self, img1, img2):
        """计算缺口偏移 这种方式成功率很高"""
        # 将图片修改为RGB模式
        img1 = img1.convert("RGB")
        img2 = img2.convert("RGB")

        # 计算差值
        diff = ImageChops.difference(img1, img2)

        # 灰度图
        diff = diff.convert("L")
        # print(self.otsu_threshold(diff))

        table = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
        # 二值化 阀值table提前计算好的
        diff = diff.point(table, \'1\')
        left = 43
        for w in range(diff.size[0] - 1, left, -1):
            lis = []
            for h in range(diff.size[1] - 2, 0, -1):
                if diff.load()[w, h] == 1:
                    lis.append(w)
                if len(lis) > 15:
                    self.error = ErrorLog(img1, img2, diff, w)
                    return w - left

接下来就是常规操作 生成轨迹 移动记好了

版权声明:本文为wzbk原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wzbk/p/14119333.html