Reputation: 745
I am facing a problem using sikuli.
In the attached toolbar image, i have same drop down menu for three time for different purpose. Using sikuli i want to click on the second dropdown menu.
I am using the below code, but the problem is while running the code, it only click on the first drop down.
My code is:
Screen screen = new Screen();
// Create object of Pattern class and specify the images path
Pattern image = new Pattern(AppConstant.IMAGE_DIR+"toolbar.png");
Pattern image2 = new Pattern(AppConstant.IMAGE_DIR+"import-button.png");
Pattern image3 = new Pattern(AppConstant.IMAGE_DIR+"dropdown.png");
//screen.wait(image.exact(), 10000);
screen.find(image);
screen.find(image2);
screen.find(image3);
Any suggestion how to do this?
Thanks
Upvotes: 0
Views: 2157
Reputation: 1165
First of All you need the Accessibility id of that element. if that element has AI(Accessibility id). Then you can iterate through the elements and access the desired index.
To get the AI in windows you can use Inspect (a Ai tool by miscrosoft).In mac you can get the AI using Appium.
If you have access to your developer source code you can find the Api there too.
ELSE : you can use the below code . Hope this will help you.
Iterator <Match> matches = screen.findAll("dropdown.png"); // s is screen
Pattern pButton = new Pattern("dropdown.png");
while (matches.hasNext()) {
Match m = matches.next();
i++;
if(i==3) {
screen.click(m); // click on drop-down
Thread.sleep(1000);
break;
}
}
Upvotes: 0
Reputation: 6910
In scenarios with multiple similar patterns, the best practice is to use the surrounding elements as a pivot. In your case, if you know that you have another unique element in the same area of the element you wish to click on, you can find that unique element first and then search for the element you actually need around the unique element.
For example, in your case, you have the blue down arrow just next to the drop down menu button you need. So you can do something like this:
ImagePath.setBundlePath("C:\\someDir\\sikulipatterns"); //This is to avoid supplying directory for each pattern
Screen screen = new Screen();
Pattern bigBlueArrowPattern = new Pattern("bigBlueArrow.png");
Pattern dropDownPattern = new Pattern("dropDownArrow.png");
Region bigBlueArrowPatternRegion = screen.find(bigBlueArrowPattern);
bigBlueArrowPatternRegion.grow(50).find(dropDownPattern).highlight(1);
Here, 50
is the margin to be added around the region, so basically extending the region around the blue arrow. I included highlight(1)
just to emphasize that the correct element has been located indeed but you should remove it and do whatever you wish with the found element.
Upvotes: 2
Reputation: 88
You can use the inbuilt findAll and getlastMatches method and then click on the one you want to.
icons = findAll(image3)
mm = list(getLastMatches())
click(mm[2])
Upvotes: 0