为什么不sikuli脚本点击图像?

时间:2013-10-27 00:27:20

标签: java sikuli

我对sikuli来说是全新的,并试图在图片截图上进行简单的点击,这对我来说已成为一种火箭科学。

仅供参考 - 我拍了谷歌徽标的截图并将其保存在我的机器上。但是,如果我获取实际的Google徽标图片网址,则该脚本可以正常运行。

这是使用图片截图的正确方法吗?

public class TestGenericButton {
    public static void main(String[] args) throws MalformedURLException {
        // Open the main page of Google Code in the default web browser
        browse(new URL("http://code.google.com"));

        // Create a screen region object that corresponds to the default monitor in full screen 
        ScreenRegion s = new DesktopScreenRegion();

        // Specify an image as the target to find on the screen
        //URL imageURL = new URL("http://code.google.com/images/code_logo.gif");
        URL imageURL = new URL("img\\google.gif");
        Target imageTarget = new ImageTarget(imageURL);

        // Wait for the target to become visible on the screen for at most 5 seconds
        // Once the target is visible, it returns a screen region object corresponding
        // to the region occupied by this target
        ScreenRegion r = s.wait(imageTarget,5000);

        // Display "Hello World" next to the found target for 3 seconds
        Canvas canvas = new DesktopCanvas();
        canvas.addLabel(r, "Hello World").display(3);

        // Click the center of the found target
        Mouse mouse = new DesktopMouse();
        mouse.rightClick(r.getCenter());
    }
}      

2 个答案:

答案 0 :(得分:0)

不,这不是创建ImageTarget的方法。您在要使用的文件中有图像,因此您需要传递File,而不是URL

the documentation,而不是

    URL imageURL = new URL("img\\google.gif");
    Target imageTarget = new ImageTarget(imageURL);

DO

    File imageFile = new File("img\\google.gif");  // assumes your working directory is set right...
    Target imageTarget = new ImageTarget(imageFile);

答案 1 :(得分:0)

提供imageURL,此方法应该点击部分:)

private void click(String image) throws FindFailed{

    Screen screen = new Screen();

    Pattern pattern = new Pattern(image).similar((float) 0.7);

    if(screen.find(pattern)!=null){ 
        screen.mouseMove(pattern); 
        screen.click(pattern); 
    }
    }