I am totally new to sikuli, and trying to perform a simple click on the image screenshot, which has become a rocket science for me.
FYI - I took a screenshot of the google logo and saved it on my machine. However, if I fetch the actual google logo image url, then the script works.
Is this the right way to use image screenshots?
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());
}
}
No, that is not the way to create an ImageTarget. You have an image in a file that you want to use, so you need to pass a File
, not a URL
.
Per the documentation , instead of
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);
Provide the imageURL, and this method should do the clicking part :)
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.