Can we have a check whether a text is present in particular region

Asked by saaliha

Can you please let me know if there is a solution to check whether a text is present in particular region .
If the text exists it should print the text exists and if it doesnt the text doesnt exists.

I 've tried it as below ,but it doesnt work:

I have a defined a list as below :
a =['apple','orange','grapes']
I have defined a region as :
t = selectRegion() # This will prompt me for the region that need be selected.
Now I want to search the selected region for the text defined in the list a.
i.e it should search for the region with the text apple and if it exists it should print apple exists else apploe does not exists.
for i in range(len(a)):

    if exists(t.find(a(i))):
        print a(i)+"exists" #if it exists it should print apple exists
        exit()
    else:
        print a(i)+"does not exists" #apple does not exists
        exit()
i+1
next i

This doesnt seems to be working .Can you please help with a solution on this ?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

found = False
for text in a:
    if t.exists(text):
        print text + " exists"
        found = True
        break
    else:
        print text + " does not exists"
if not found:
    print "none of given texts found"

Revision history for this message
saaliha (saalihaiqbal) said :
#2

Thanks for the quick revert,RaiMan.

I tried it out and it gave me the output as
apple does not exists

orange does not exists

grapes does not exists
none of given texts found
Where as the texts 'apple','orange','grapes' actually exists in the GUI
====================================
I changed the above to :
t = selectRegion()
a = 'grapes'
found = False
for text in a:
    if t.exists(text):
        print text + " exists"
        found = True
        break
    else:
        print text + " does not exists"
if not found:
    print "none of given texts found"

The output received is as:
g exists.
Here it reads the first charachter of the text and matches it.
====================
Any clue on how to proceed with it so that it will search for the entire text and check for its present in the GUI and give the output as "grapes exists" "apple exists " and "orange exists"

Revision history for this message
RaiMan (raimund-hocke) said :
#3

--- Here it reads the first charachter of the text and matches it.
giving a string to for x in y, will loop for each character in y

so you would have to say
a = ('grapes',)
or
a = ['grapes']
or
a = list('grapes')

--- words not found
Since the text feature is still very unstable, you have to check, what Sikuli reads from the screen.
So first use:
print selectRegion().text()

and select as region one of the words with each run.

the output will tell you what Sikuli sees ;-)

If it is not as expected, you will not have any chance with the text feature on your text font.

BTW: are you on version 1.0.1 ??

Revision history for this message
saaliha (saalihaiqbal) said :
#4

I'm on version 1.0.1.

I tried printing it :
t = selectRegion()
a = ('grapes')
found = False
for text in a:
    if t.exists(text):
        print t.text()
        print text + " exists"
        found = True
        break
    else:
        print text + " does not exists"
if not found:
    print "none of given texts found"

>> The output was:
app||e #output for print t.text().Its reading the letters 'll' as ||
ora nge #output for print t.text()
grapes #output for print t.text()
g exits #print text + " exists"
=======================
I think as you said trying to read text may not be fruitful.
Or may be I can think to write the output of print t.text() to excel using xlwt and later modfy the same using xlutils by applying some formula and convert the same to the nearest matching strings we are intereseted in.
But I 'm really thinking whether its really worth the effort in reading the text especially when its said in previous reply as the text extraction is still unstable..
Any thing which you would like add on this ,like any alternatives ,please?

Revision history for this message
RaiMan (raimund-hocke) said :
#5

so at least grapes should be found.

pls check again:

so you would have to say
a = ('grapes',) # mind the trailing comma!
or
a = ['grapes']
or
a = list('grapes')

Revision history for this message
RaiMan (raimund-hocke) said :
#6

if the text is selectable, you might simulate a copy (ctrl-c) and use
Env.getClipboard() to get the text and check it

Revision history for this message
saaliha (saalihaiqbal) said :
#7

With selectable text , I have already tried with Env.getClipboard() and was working perfect.

But this particular problem is with non- selectable GUI content.
The above mentioned 3 options also didnt worked.

I think the below link also discusses the same :
https://answers.launchpad.net/sikuli/+question/139475

Revision history for this message
RaiMan (raimund-hocke) said :
#8

So I guess, this will not work currently.

But to approve it, you might send me a sample screenshot with the texts silently to my mail at https://launchpad.net/~raimund-hocke.

Can you help with this problem?

Provide an answer of your own, or ask saaliha for more information if necessary.

To post a message you must log in.