Jump to content

Using WinGetPos and WinGetTitle with Microsoft Edge sometimes returns incorrect values


Recommended Posts

I have a script that opens Microsoft Edge and then uses WinGetPos and WinGetTitle to get the size and name of the window. I am using the "Chrome_WidgetWin_1" class to do this as the title is not always the same. I have the code loop and do this 5 times. Randomly it will return a size much smaller than the window (it should be 1936x1152, and the incorrect numbers are like 64x64 or 478x64) and a blank name. I added a ScreenCapture_CaptureWnd function to take an image of the window to see what it is when it gets the wrong info. It took an image of the start button and the bottom left part of the  Edge window (the window is maximized. Is Edge opening a hidden window or something that has the same class and my functions are using it instead of the actual visible Edge window?

Here is a snippet of the code and the screenshot I got of one of the times it returned a smaller size.

For $i = 1 to 5
         ShellExecute("https://app.powerbi.com/groups/me/list")
         Sleep(6000)
         WinSetState("[CLASS:Chrome_WidgetWin_1]","", @SW_MAXIMIZE)

         ;Get window size and position
         Local $aPos = WinGetPos("[CLASS:Chrome_WidgetWin_1]")
         Local $sEdge1 = WinGetTitle("[CLASS:Chrome_WidgetWin_1]")
         Local $hWnd = WinGetHandle("[CLASS:Chrome_WidgetWin_1]")
         Global $WLeft = 0, $WTop = 0, $WRight = $aPos[2], $WBottom = $aPos[3]
         FileWrite($output_file, "Edge position: Left " & $WLeft & "  Top " & $WTop & "  Right " & $WRight & "  Bottom " & $WBottom & @CRLF); For Troubleshooting
         FileWrite($output_file, "Edge Name " & $sEdge1 & @CRLF); For Troubleshooting
         _ScreenCapture_CaptureWnd(@ScriptDir & "\Edge_screenshot.jpg", $hWnd); For Troubleshooting
         Sleep(2000)
         WinClose("[CLASS:Chrome_WidgetWin_1]")
Next

Edge_screenshot.jpg.fa22c4e996386cfe6d96b5b99d42b77e.jpg

Link to comment
Share on other sites

I would recommend that you slightly revise your code to ensure that you are accessing the same window with each command --

For $i = 1 to 5
         ShellExecute("https://app.powerbi.com/groups/me/list")
         Sleep(6000)

        ; get window handle / maximize window
         Local $hWnd = WinGetHandle("[CLASS:Chrome_WidgetWin_1]")
         WinSetState($hWnd,"", @SW_MAXIMIZE)
          
         ;Get window size and position
        Local $aPos = WinGetPos($hWnd)
         Local $sEdge1 = WinGetTitle($hWnd)
         Global $WLeft = 0, $WTop = 0, $WRight = $aPos[2], $WBottom = $aPos[3]
         FileWrite($output_file, "Edge position: Left " & $WLeft & "  Top " & $WTop & "  Right " & $WRight & "  Bottom " & $WBottom & @CRLF); For Troubleshooting
         FileWrite($output_file, "Edge Name " & $sEdge1 & @CRLF); For Troubleshooting
         _ScreenCapture_CaptureWnd(@ScriptDir & "\Edge_screenshot.jpg", $hWnd); For Troubleshooting
         Sleep(2000)
         WinClose("[CLASS:Chrome_WidgetWin_1]")
Next

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...