美文网首页
sikulix使用方法简介

sikulix使用方法简介

作者: lk_erzanml | 来源:发表于2023-07-27 16:43 被阅读0次

所需要的附件:
链接:https://pan.baidu.com/s/1oE0der_1UY3CZNf4HpWdAg?pwd=nn9m
提取码:nn9m

import time
import traceback

import jpype
from jpype import *

class SikuliOperate(object):

    def __init__(self):
        try:
            jvmPath = r".\jre1.8.0_241\bin\server\jvm.dll"  # 当前文件夹下的jvm.dll文件
            # 启动sikuli jar包,也在当前文件夹下
            jpype.startJVM(jvmPath, "-ea", "-Djava.class.path=./sikulix.jar")
            Screen = JClass("org.sikuli.script.Screen")
            kd = jpype.JClass("org.sikuli.script.RobotDesktop")
            self.screen = Screen()
            self.kd = kd()
        except:
            traceback.print_exc()

    def sikuli_move(self,x,y):
        # 移动到某坐标
        # 继承IRobot,还有如下方法
        #    void keyPress(int keycode);
        #    void keyRelease(int keycode);
        #    void pressModifiers(int modifiers);
        #    void releaseModifiers(int modifiers);
        #    void typeChar(char character, KeyMode mode);
        #    void mouseMove(int x, int y);
        #    void mousePress(int buttons);
        #    void mouseRelease(int buttons);
        self.kd.mouseMove(x,y)

    def sikuli_click(self, image_path):
        """
        通过图片点击
        :param image_path:  图片路径
        :return:
        """
        try:
            self.screen.click(image_path)  #还有个参数用来判断是鼠标左中右点击
        except:
            return traceback.format_exc()

    def sikuli_click_in(self, image_path1,image_path2):
        """
        在一定范围内点击图片
        :param image_path1,2:  图片路径 在范围1里点击2
        :return:
        """
        try:
            self.screen.click_in(image_path1,image_path2)
        except:
            return traceback.format_exc()

    def sikuli_send_keys(self, image_path, text):
        """
        获取指定组件并输入内容
        :param image_path: 图片路径
        :param text: 内容
        :return:
        """
        try:
            self.screen.type(image_path, text)
        except:
            traceback.format_exc()

    def sikuli_double_click(self, image_path):
        """
        双击组件
        :param image_path: 图片路径
        :return:
        """
        try:
            self.screen.doubleClick(image_path)
        except:
            traceback.format_exc()

    def sikuli_right_click(self, image_path):
        """
        右击
        :param image_path: 图片路径
        :return:
        """
        try:
            self.screen.rightClick(image_path)
        except:
            traceback.format_exc()

    def sikuli_drag_drop(self, image_path1, image_path2):
        """
        拖拽组件
        :param image_path1: 拖拽组件图片路径
        :param image_path2: 拖拽后移动位置
        :return:
        """
        try:
            self.screen.dragDrop(image_path1, image_path2)
        except:
            traceback.format_exc()

    def sikuli_shutdowm(self):
        """
        关闭sikuli
        :return:
        """
        try:
            jpype.shutdownJVM()
        except:
            traceback.format_exc()

    def exists(self, image_path):
        """
        查看图片在当前页面是否存在
        :param image_path: 图片路径
        :return: Ture or False
        """
        try:
            res = self.screen.exists(image_path)
            if res is None:
                return False,None
            else:
                return True,None
        except:
            return False,traceback.format_exc()

    def wait(self, image_path, timeout):
        """
        等待图片出现
        :param image_path: 图片路径
        timeout 超时时间 单位 秒
        :return: Ture or False
        """
        try:
            for i in range(0,timeout):
                res = self.exists(image_path)
                if res is None or res is False:
                    time.sleep(1)
                    continue
                else:
                    return True
            return False
        except:
            return False,traceback.format_exc()

    def wait_click(self, image_path, timeout):
        """
        等待图片出现并单击
        :param image_path: 图片路径
        timeout 超时时间 单位 秒
        :return: Ture or False
        """
        res = self.wait(image_path,timeout)
        if res:
            self.sikuli_click(image_path)
            time.sleep(1)
            return True
        else:
            return False

    def wait_double_click(self, image_path, timeout):
        """
        等待图片出现并双击
        :param image_path: 图片路径
        timeout 超时时间 单位 秒
        :return: Ture or False
        """
        res = self.wait(image_path,timeout)
        if res:
            self.sikuli_double_click(image_path)
            time.sleep(1)
            return True
        else:
            return False

    def find_img1_img2(self,img1,img2):
        """
        先从屏幕查到img1,然后从img1查到img2
        返回Match,Mathch对象继承Region对象,所以可对这个区域点击等操作
        """
        try:
            x = screen.find(img1).find(img2)
            return True, x
        except:
            return False, traceback.format_exc()


if __name__ == "__main__":
    obj = SikuliOperate()

    # obj.sikuli_click("批注 2023-07-28 143758.png")  # 点击

    screen = obj.screen
    #
    # print(screen.x,screen.y,screen.w,screen.h)  #获取坐标,宽高
    #
    # a=screen.getCenter()
    #
    # print(type(screen.getCenter()))
    #
    # c=a.offset(10,10)
    #
    # print(type(c.toString()))

    # x=screen.find("批注 2023-07-28 143758.png")
    #
    # print(x)
    #
    # x = screen.findAll("批注 2023-07-28 143758.png")
    #
    # for z in x:
    #     print(z)

    # x=screen.find("da.PNG")
    #
    # print(x)

    # x2=x.find("interllij.PNG")
    #
    # print(x2)
    #
    # x2.doubleClick()

    # x3=screen.find("interllij.PNG")
    #
    # print(x3)
    #
    # x3.doubleClick()
    # x4=screen.waitVanish("interllij.PNG",3)  #在3s等待某个图片消失
    #
    # print(x4)

    # screen.wheel(123,12)

    # obj.sikuli_move(100,200)

    flag,x=obj.find_img1_img2("da.PNG","xiao.PNG")
    print(flag,x)
    if flag:
        x.doubleClick()



相关文章

网友评论

      本文标题:sikulix使用方法简介

      本文链接:https://www.haomeiwen.com/subject/wsalpdtx.html