Can't set focus in new popup chrome window using "setFocus"

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
uniadv
Junior Coder
Posts: 29
Joined: Tue Aug 04, 2020 2:16 am

Can't set focus in new popup chrome window using "setFocus"

Post by uniadv » Tue Aug 04, 2020 3:33 am

Hi,
When I run my macro, it open ups a chrome window which has bunch of rows. On clicking each row (by Macro Scheduler), it further open a new popup window with same URL for all but different title. This new popup window is always on top.

It's title is like: "{{random title}} - Google Chrome"

When I am using "setFocus" and try to use OCR it doesn't give any value. Maybe i need to set focus first.

I used these in three different example
  • SetFocus>* - Google Chrome
  • SetFocus>Google Chrome*
  • SetFocus>google*
And then these too
  • ChromeGetInfo>session_id,url,theURL
  • ChromeGetInfo>session_id,title,theTitle

But the MessageModel always gives the main window URL and Title not the popup window data. And OCR result is always empty.


Any idea where I'm wrong?

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Can't set focus in new popup chrome window using "setFocus"

Post by Dorian (MJT support) » Tue Aug 04, 2020 9:46 am

You shouldn't need to set focus if you use OCRWindow for your OCR. You should be able to get the window title with GetActiveWindow.

Code: Select all

//Uncomment this if the popup is a child window
//Let>GAW_TYPE=1

GetActiveWindow>WinTitle,XX,YY,WW,HH

//If you wish, you can now set focus
SetFocus>%WinTitle%

OCRWindow>%WinTitle%,strText
MessageModal>strText
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
uniadv
Junior Coder
Posts: 29
Joined: Tue Aug 04, 2020 2:16 am

Re: Can't set focus in new popup chrome window using "setFocus"

Post by uniadv » Tue Aug 04, 2020 1:35 pm

Thank Dorian for reply, :D

That code partially worked, but while using "OCRWindow" it give everything in messed-up form. Every new window is slightly different so can't use "OCR Screen Rectangle" either.

I think "xpath" can be useful to get the text from that new popup chrome window. But for that, the child window need to be focused/activate. Here i'm unable to use xpath in new child window.

Is there any way to focus the child window so xpath would work?

The one more complication is that, every time the title name is different and few title name have clock in it which changes every second.

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Can't set focus in new popup chrome window using "setFocus"

Post by Marcus Tettmar » Wed Aug 05, 2020 9:47 am

We'll be adding a function to switch windows in Chrome/Edge soon.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
uniadv
Junior Coder
Posts: 29
Joined: Tue Aug 04, 2020 2:16 am

Re: Can't set focus in new popup chrome window using "setFocus"

Post by uniadv » Wed Aug 05, 2020 3:35 pm

That would be great update to Macro Scheduler particularly for chrome users. :D :D

So when can we expect to get this update?

If it will take time then any alternative to switch chrome browser to its child window?

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Can't set focus in new popup chrome window using "setFocus"

Post by Marcus Tettmar » Wed Aug 05, 2020 6:55 pm

Sorry no timescales as yet.

But, yes, you can extend the ChromeDriver/EdgeDriver functions with standard HTTPRequest calls. Here are two functions. One which will get a list of windows and their internal names and another which can use one of these names to switch the browsing context:

Code: Select all

//returns a list of window names belonging to the current session
SRT>GetWindowHandles
  HTTPRequest>http://localhost:9515/session/%GetWindowHandles_Var_1%/window/handles,,GET,,theResult
END>GetWindowHandles


//takes session ID and window name to switch browsing context to
SRT>SwitchWindow
  Let>HTTP_POSTJSON=1
  Let>body= { "name": "%SwitchWindow_Var_2%" }
  //if you are not using the standard port and have changed it with CHROMEDRIVER_PORT then change the port number here accordingly
  HTTPRequest>http://localhost:9515/session/%SwitchWindow_Var_1%/window,,POST,body,theResult
END>SwitchWindow
So, this works much like the built in function ChromeSwitchFrame.

Imagine you have a web page which opens another window when you click on a link. The following example will find the name of the new window and then switch browsing context to it, so that subsequent actions apply to that window instead of the first one:

Code: Select all

Let>CHROMEDRIVER_EXE=c:\drive_d\chromedriver.exe
ChromeStart>session_id
ChromeNavigate>session_id,url,https://www.blabla.com/somepage/test.html

GoSub>GetWindowHandles,session_id

//imagine we have a link with ID 'thelink' and clicking it produces a new window ... 
ChromeFindElements>session_id,id,thelink,elements
ChromeElementAction>session_id,elements_1,click

Wait>2

//so now we can get a list of windows 
GoSub>GetWindowHandles,session_id

//assuming we had only one window to start with so we'll get the second (index 1) item here. What you COULD do if you didn't know how many you had is call GetWindowHandles BEFORE clicking the link, then compare and use the new handle which didn't exist previously ... 
JSONParse>theResult,$.value[1],newHandle

//now switch browsing context
GoSub>SwitchWindow,session_id,newHandle_1

//now the Chrome functions will find/act on elements inside the NEW window. Remember to switch context back later if needed .... 
ChromeFindElements>session_id,tag name,a,allAs

SRT>GetWindowHandles
  HTTPRequest>http://localhost:9515/session/%GetWindowHandles_Var_1%/window/handles,,GET,,theResult
END>GetWindowHandles

//takes session ID and window name to switch browsing context to
SRT>SwitchWindow
  Let>HTTP_POSTJSON=1
  Let>body= { "name": "%SwitchWindow_Var_2%" }
  //if you are not using the standard port and have changed it with CHROMEDRIVER_PORT then change the port number here accordingly
  HTTPRequest>http://localhost:9515/session/%SwitchWindow_Var_1%/window,,POST,body,theResult
END>SwitchWindow
Hope this helps.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
uniadv
Junior Coder
Posts: 29
Joined: Tue Aug 04, 2020 2:16 am

Re: Can't set focus in new popup chrome window using "setFocus"

Post by uniadv » Fri Aug 07, 2020 8:59 am

Thank you Marcus Tettmar, It worked smoothly :D :D ❤️

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts