Activate Tab on Chrome

%3CLINGO-SUB%20id%3D%22lingo-sub-2033915%22%20slang%3D%22en-US%22%3EActivate%20Tab%20on%20Chrome%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2033915%22%20slang%3D%22en-US%22%3E%3CP%3EAfter%20launch%20a%20URL%20on%20a%20single%20tab%20of%20Chrome%2C%20the%20following%20will%20work%20just%20fine.%20But%20It%20will%20not%20work%20if%20Google.com%20is%20on%20an%20inactive%20tab%20of%20multiple%20tabs.%20Is%20there%20other%20way%20to%20do%20that%3F%20Thanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EStart-Process%20chrome%20(%22%3CA%20href%3D%22https%3A%2F%2Fwww.google.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fwww.google.com%3C%2FA%3E%22)%3CBR%20%2F%3EStart-Sleep%20-Seconds%203%3C%2FP%3E%3CP%3E%24p%20%3D%20Get-Process%20Chrome%20%7C%20%3F%20%7B%20%24_.MainWindowTitle.Contains('Google')%20%7D%3C%2FP%3E%3CP%3E%5BMicrosoft.VisualBasic.Interaction%5D%3A%3AAppActivate(%24p.Id)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPS%205%20on%20Windows%2010%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2033915%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

After launch a URL on a single tab of Chrome, the following will work just fine. But It will not work if Google.com is on an inactive tab of multiple tabs. Is there other way to do that? Thanks.

 

Start-Process chrome ("https://www.google.com")
Start-Sleep -Seconds 3

$p = Get-Process Chrome | ? { $_.MainWindowTitle.Contains('Google') }

[Microsoft.VisualBasic.Interaction]::AppActivate($p.Id)

 

PS 5 on Windows 10

2 Replies

Hi @Yuansheng Zhang 

bases on what I see, all Google Chrome Tabs are Child-Process to Google Chrome it self

Check this

image.png

even with the code you provide, if you have multiple Chrome Tabl open, it will only get the Main process

The result from your Code.

farismalaeb_0-1609876701956.png

Did you notice the same Process ID?

Google is changing the Main Windows Title based on the Active Tab, this is why you are facing this issue, 

I tried to dig via Process Explorer and see if I can get anything via the command ID or whatever parameter assigned to the tab but did not find anything.

it seems that other tabs don't hold a Title until activation, Check this

(Get-Process Chrome).foreach{$_.MainWindowTitle}

the only result you will get is the active Window Title, and a lot of other null values

 

 

Small hint that might help, Check SysInternal TCPView, get the output from the console, you will get all the destination that chrome is connecting to, yet the process maybe a bit slow, but it can work as a workaround.