How to access chrome child window

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
mnaeem
Junior Coder
Posts: 30
Joined: Wed Mar 10, 2021 5:05 pm

How to access chrome child window

Post by mnaeem » Sat Oct 23, 2021 4:28 pm

Hello,
I am trying to access a child window, kindly see this picture https://1drv.ms/u/s!AoNHROZr6p0PkGLaWzg ... F?e=PzV0Z5

Actually there are 2 buttons which are hidden in previously shown picture. They are visible in this picture https://1drv.ms/u/s!AoNHROZr6p0PkGMcfP2 ... e?e=tAfjQ5 I need to click the I Agree button shown in the later picture. To access the I Agree button I am trying this below xpath.

Code: Select all

ChromeFindElements>session_id,xpath,//*[contains(text(),'I Agree' )]//parent::tp-yt-paper-button//parent::a,elements
For your further reference I am also providing the html picture https://1drv.ms/u/s!AoNHROZr6p0PkGQyS_S ... N?e=SdxAMg for better understanding.

Appreciate your guidance.

Thanks

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

Re: How to access chrome child window

Post by Dorian (MJT support) » Mon Oct 25, 2021 10:07 am

Those ones can be quite fiddly. You have to switch context, interact, then switch it back again. You can see the original explanation here.

This does what you require :

Code: Select all

let>TheURL=https://www.youtube.com/
Let>CHROMEDRIVER_EXE=c:\chromedriver.exe

ChromeStart>session_id
put>session_id
ChromeNavigate>session_id,url,%TheURL%

//Get current Windows
GoSub>GetWindowHandles,session_id

//Wait to ensure window has popped up (may need to increase this)
wait>1

//Get a list of windows
GoSub>GetWindowHandles,session_id
JSONParse>theResult,$.value[1],newHandle
JSONParse>theResult,$.value[0],mainHandle

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

//Click the button
ChromeFindElements>session_id,xpath,//yt-formatted-string[@class='style-scope ytd-button-renderer style-primary size-default'],el
ChromeElementAction>session_id,el_2,click

//Switch back to main window
GoSub>SwitchWindow,session_id,mainHandle_1

//
//continue script
//

//Subroutines
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
Yes, we have a Custom Scripting Service. Message me or go here

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

Re: How to access chrome child window

Post by Marcus Tettmar » Tue Dec 14, 2021 12:01 pm

Version 15.0.20 now has native ChromeGetWIndows and ChromeSwitchWindow functions.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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