2015-01-14 96 views
1

目的:要在網頁中單擊所有「星級」通過該行中移動第一和然後列SikuliSikuli:多個圖標排序

示例:星星被佈置在像一個柵格所以:

* * * * * 
* * * * * 
* * * * * 
* * * * * 

編輯:這是被點擊的順序:

1 2 3 4 5 
6 7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25 

首先點擊左上角的那個,然後點右側的那個,等等。接下來轉到第二行的左上角並重復。

我當前的代碼:

def by_x(match): 
    return match.x 
def by_y(match): 
    return match.y 
stars = findAll("imgOfStar") 
sorted_stars_x = sorted(stars, key=by_x) 
sorted_stars_y = sorted(stars, key=by_y) 
for icon in sorted_stars_x: 
    for icon2 in sorted_stars_y: 
     click("imgOfStar") 

回答

1

這可能不是最優雅的方式,但它是我能想到的第一件事就是:

def by_y(match): 
    return match.y 
stars = findAll(imageOfStars) 
sorted_stars_y = sorted(stars, key=by_y) 
finalStars = [] 
count = 0 
for x in range(5): #if you know your grid is 5x5 
    finalStars.append(sorted(sorted_stars_y[count:count + 5])) #see explanation, if needed 
    count += 5 
for x in finalStars: 
    click(x) 

解釋:從您的例子中,第一五顆星應該有匹配的y值,即它們應該都是最上面的一行。所以,現在,您只需對它們的x值進行排序,然後將它們附加到列表中,然後轉到下一個五行,等等。

如果你的網格的大小事先不知道,你可以做到這一點的幾個不同ways-- 如果您的網格總是完美的正方形,你會發現你的星數的平方根:

import math #or import sqrt from math, if the square root is the only math function you need. 
def by_y(match): 
    return match.y 
stars = findAll(imageOfStars) 
sorted_stars_y = sorted(stars, key=by_y) 
finalStars = [] 
count = 0 
rows = math.sqrt(len(stars)) 
for x in range(rows): 
    finalStars.append(sorted(sorted_stars_y[count:count + rows])) 
    count += rows 

如果您的網格是不完美的正方形,還有一些其他的事情可以做,但除非這是你在找什麼,這個答案已經變得有點長,所以我們要保存以後討論:)

編輯: 既然你知道你的列數始終是5,你會發現數量像這樣的列:

rows = (len(stars)/5) 
rowCount = 0 
count = 0 

然後你就可以使用while循環通過你的明星迭代:

while rowCount < rows: 
    finalStars.append(sorted(sorted_Stars_y[count:count+ 5])) 
    count += 5 
    rowCount += 1 

畢竟是說,做,這將會把工作給你做,但從@Tenzin答案:)

+0

謝謝,答案。但我不知道行數。我只知道列數,即5。 – WhiteFlameAB

1

您可以AA的定義,你搞清楚,你怎麼樣通過星移動更優雅。屏幕本身有一個x.y的位置。要從左上角開始並在右下角結束,您需要 match.y,match.x。

然後,你需要的findAll( 「stars.png」)的恆星。 你按照你定義的順序去排序這些星星。

然後你使用一個for循環做明星的東西。

示例代碼:

class Stars(): 
    def order(match): 
      return match.y, match.x 
    # Find all icons 
    icons = findAll("stars.png") 
    # Sort all the stars. 
    sorted_icons = sorted(icons, key=order) 
    # Click on every star. 
    for icon in sorted_icons: 
      click(icon)