Focus function call on Firefox app causes focus to be lost on app

Asked by Jason Smith

OS: Windows 7 64-bit

Code snippet:

location = "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"
firefox = App.open(location)
wait(5)
firefox.focus()
wait(5)

Expected:

Firefox should be in focus.

Actual:

On App.Open, Firefox is in focus. But when firefox.focus() is called, Firefox is taken out of focus.

Question information

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

Handling of Firefox sessions (or other modern singleProcess/multiWindow applications) on Windows is a bit tricky.

You approach especially when rerun in the IDE does not work generally.
The current implementation of the App class in Windows is not able to handle Firefox as expected.

Try this instead, which works for me (Win 7 32Bit):

ff = r"C:\Program Files\Mozilla Firefox\firefox.exe"
ffA = App("Mozilla Firefox")
if not ffA.window():
    App.open(ff)
for i in range(10):
    if ffA.window(): break
    wait(1)
ffA.focus()
App.focusedWindow().highlight(2) # only to check

If at least one FF window is already open, the frontmost one is focused.
If not, we open FF and wait for a window (FF has many windows open for performance reasons, but most of them are invisible normally) and finally focus.

As you can see, only the approach, using the window title to identify the "app", works. If you have more than one FF window open, you have to use a more specific part of the window title.

Just to get an impression, run this snippet 2 or 3 times in the IDE:

ff = r"C:\Program Files\Mozilla Firefox\firefox.exe" # opens a new ff window
ffA = App.open(ff)
wait(3)
for i in range(100):
    w = ffA.window(i)
    if not w: break
    print w

and look at the list of windows ;-)

Revision history for this message
Jason Smith (jds2501) said :
#2

Executing the first code snippet results in the following error:

[error] Stopped
[error] An error occurs at line 9
[error] Error message: Traceback (most recent call last):
 File "C:\Users\jsmith\AppData\Local\Temp\sikuli-tmp3389009394525568436.py", line 9, in
 App.focusedWindow().highlight(2) # only to check
AttributeError: 'NoneType' object has no attribute 'highlight'

Also, executing that focus operation still appears to put the Firefox window out of focus.

Revision history for this message
Jason Smith (jds2501) said :
#3

Just tested this on a Windows 7 32-bit OS. Didn't get the error trace above, but the focus operation still appears to put Firefox out of focus (watch the top portion of the window during the test case...it unhighlights when focus is called on the Firefox app). Could this be a problem in Firefox specifically (in that focus is implemented incorrectly)? Or is this a general Windows problem?

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

I will again have a look at this in about 2 hours.

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

I tested again with the following snippet:

ff = r"C:\Program Files\Mozilla Firefox\firefox.exe"
ffA = App("Mozilla Firefox")
if not ffA.window():
    App.open(ff)
for i in range(10):
    if ffA.window(): break
    wait(1)
ffA.focus()
type("l", KeyModifier.CTRL)
paste("google.de")
type(Key.ENTER)
wait(2)

In all cases (FF open or not) finally I have the google webpage opened.

I am using FF 8 and it is Sikuli X-1.0rc3 (newest version from http://sikuli.org/hudson).

Revision history for this message
Jason Smith (jds2501) said :
#6

Got my code working experimenting with your snippet above against mine. The underlying problem I might have been hitting was that when I was looking for a particular image on screen that I pointed to something that was not the application, and focused in on that. Thanks for the help.

Revision history for this message
Bharathi A (rathisoft) said :
#7

In my case - I need to find the path of firefox first.

For eg: In one PC - firefox.exe is located under program files and in another pc firefox.exe is located under appdata folder.

I tried something like this but found to be not working:

import os
for r,d,f in os.walk("c:\\"):
    for files in f:
         if files == "firefox.exe":
             print os.path.join(r,files)
         else:
             print ("Not found")
             exit(1)

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

@rathisoft
- please do not ask questions for new topics as comments in old questions.

os.walk is only available in Python 3+

SikuliX is language level 2.7, where you have only os.path.walk