Jump to content

Can't get handle of Browser File Open dialog


AI123
 Share

Recommended Posts

Hello, after I failed to write a script to upload a file without a file open dialog
(https://www.autoitscript.com/forum/topic/204400-how-to-suppress-file-browser-upload-dialog)
now I try it as the second best solution with browser open file dialog.

After the second click the the file open dialog appears:

; First Click on "Weitere Designs hochladen oder einfach hierherziehen"
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/div[1]/ui-view/div/div/div/div/div[2]/div[1]/a/div/h4[1]")
_WD_ElementAction($sSession, $sElement, 'click')
Sleep(2000)

; Second Click on "Design hochladen" -> file open dialog appears
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[1]/div/div/div")

;_WD_ElementAction($sSession, $sElement, 'click')
;Sleep(2000)

;Wait 15sec for file dialog (Class #32770) with title 'Öffnen'
$hWnd = WinWait("[CLASS:#32770]","Öffnen",15)
;ConsoleWrite("$hWnd = " & $hWnd & @CRLF)

_WD_ElementAction($sSession, $sElement, 'click')
Sleep(2000)

ConsoleWrite("$hWnd = " & $hWnd & @CRLF)

But the problem is that I get always 0 for $hWnd. So I think the file open window can't be found by AutoIt.
I'm not sure if a handle can be found of a window which is yet open.
So I tested $hWnd = WinWait("[CLASS:#32770]","Öffnen",15) for two cases.
First if file open dialog is yet open and second I clicked for file open dialog after I
set $hWnd = WinWait("[CLASS:#32770]","Öffnen",15).

In both cases $hWnd is 0.

So why can't the file open window not be found?

Edited by AI123
Link to comment
Share on other sites

Link to comment
Share on other sites

26 minutes ago, Nine said:

Second parameter of the WinWait is text not title.  Try removing second parameter and replace it with "".

Then you will need to use Control* to interact with the Window.

 

That works, thank you.

Link to comment
Share on other sites

Yesterday all worked right with this script:

;https://dev.to/valeria/how-to-automate-file-upload-testing-with-autoit-and-protractor-d94
Sleep(2000)
;then I click on an element to open browser file open dialog

;Wait 15sec for file dialog (Class #32770) with title 'Open'
$hWnd = WinWait("[CLASS:#32770]","",15)
ControlFocus($hWnd, "", "[CLASS:Edit; INSTANCE:1]")
ControlSetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]", "testtext")
ConsoleWrite("$hWnd = " & $hWnd & @CRLF)

;My experience has shown that ControlClick() works unstable. From time to time a button is
;not clicked by unknown reason.
;ControlClick($hWnd, "", "[CLASS:Button; INSTANCE:1]")
;I have solved this problem by using mouse actions such as mouse move and mouse click.

;Get Open button position.
;ControlGetPosition returs x and y coordinates of high left corner of the button.
Local $pos[2]
$pos = ControlGetPos($hWnd, "", "[CLASS:Button; INSTANCE:1]")

;Set the way coords will be used in the mouse functions, either absolute coords or coords relative
;to the current active window

;2 = relative coords to the client area of the active window
AutoItSetOption("MouseCoordMode", 2)

;Define $x and $y variables. To prevent some random failures move the mouse from high left corner
;to another button space by adding 10px to $x and $y. Feel free in playing with coordinate values!
$x=$pos[0] + 10
$y=$pos[1] + 10

MouseClick($MOUSE_CLICK_LEFT, $x, $y, 1);
ConsoleWrite("$hWnd = " & $hWnd & @CRLF)

But today the text "testtext" is not set in the edit field and the click is done on a wrong position now (not on the button). The handle for the file open dialog is still found ($hWnd = 0x00030222). But the rest that worked yesterday failed today although I can't remember to have changed something on the code.

So what could be the problem?

Edited by AI123
Link to comment
Share on other sites

Put some error handling after each statement (return value & @error).  ConsoleWrite the content of all variables after they are set.  This way you will have a better idea if there is an error somewhere or if a variable is badly set.

Link to comment
Share on other sites

ControlFocus returns now code for failure although the finder tool has the same value I set ([CLASS:Edit; INSTANCE:1]) (code $hWnd = WinWait("[CLASS:#32770]","",15) can't be seen here because it's more on top but it is set):ControlFocus.jpg.b8c096bd18b9f26058c561dfdc235caa.jpg

I get this on the console:

ControlFocus $success = 0

 

Edited by AI123
Link to comment
Share on other sites

Well you commented out the line where you set $hWnd !

Next time post full code with full console output.  Do not post images, it doesn't help us at all except for the autoit info tool (of course).

 

Link to comment
Share on other sites

This is the full code. I set $hWnd there:

;https://dev.to/valeria/how-to-automate-file-upload-testing-with-autoit-and-protractor-d94

Sleep(2000) ; muss drinbleiben, sonst funktioniert es nicht!

; First Click on "Weitere Designs hochladen oder einfach hierherziehen"
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/div[1]/ui-view/div/div/div/div/div[2]/div[1]/a/div/h4[1]")
_WD_ElementAction($sSession, $sElement, 'click')
Sleep(2000)

; Second Click on "Design hochladen"
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[1]/div/div/div")

_WD_ElementAction($sSession, $sElement, 'click')
Sleep(2000)

;#cs
;Wait 15sec for file dialog (Class #32770) with title 'Open'
;$hWnd = WinWait("[CLASS:#32770]","Öffnen",15) ;Ö&ffnen
;Opt("WinTitleMatchMode", 1)
$hWnd = WinWait("[CLASS:#32770]","",15)
;ConsoleWrite("$hWnd = " & $hWnd & @CRLF)

;_WD_ElementAction($sSession, $sElement, 'click')
;Sleep(2000)

;ConsoleWrite("$hWnd = " & $hWnd & @CRLF)

;$hWnd = WinWait("[CLASS:#32770]","Open",15)
;$hWnd = WinWait("[CLASS:#32770]","Open",5)

Sleep(5000)
Local $success
;$success = ControlFocus($hWnd, "", "[CLASS:Edit; INSTANCE:1]")
$success = ControlFocus("Öffnen", "", "[CLASS:Edit; INSTANCE:1]")
ConsoleWrite("ControlFocus $success = " & $success & @CRLF)

;ControlClick($hWnd, "", "[CLASS:Edit; INSTANCE:1]")
;Sleep(1000)

;ControlSetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]", $CmdLine[1])
;ConsoleWrite("Filepath+Imagename = " & $filepath & $local_imagename & @CRLF)

;https://autoit.de/thread/17897-controlsend-und-windows7-explorer/
;Local $cHwnd = ControlGetHandle($hWnd, "", "[CLASS:Edit; INSTANCE:1]")
;ControlSetText($hWnd, "", $cHwnd, $filepath & "\" & $local_imagename)

;ControlSetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]", $filepath & "\" & $local_imagename)
ControlSetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]", "testtext")
;ControlSend($hWnd, "", "[CLASS:Edit; INSTANCE:1]", "testtext")
;Sleep(1000)

;ConsoleWrite("$hWnd = " & $hWnd & @CRLF)

;My experience has shown that ControlClick() works unstable. From time to time a button is
;not clicked by unknown reason.
;ControlClick($hWnd, "", "[CLASS:Button; INSTANCE:1]")
;I have solved this problem by using mouse actions such as mouse move and mouse click.

;#cs
;Get Open button position.
;ControlGetPosition returs x and y coordinates of high left corner of the button.
Local $pos[2]
$pos = ControlGetPos($hWnd, "", "[CLASS:Button; INSTANCE:1]")

;Set the way coords will be used in the mouse functions, either absolute coords or coords relative
;to the current active window

;2 = relative coords to the client area of the active window
AutoItSetOption("MouseCoordMode", 2)

;Define $x and $y variables. To prevent some random failures move the mouse from high left corner
;to another button space by adding 10px to $x and $y. Feel free in playing with coordinate values!
$x=$pos[0] + 10
$y=$pos[1] + 10

MouseClick($MOUSE_CLICK_LEFT, $x, $y, 1);
ConsoleWrite("$hWnd = " & $hWnd & @CRLF)
;#ce

Both of this two failed:

;$success = ControlFocus($hWnd, "", "[CLASS:Edit; INSTANCE:1]")
$success = ControlFocus("Öffnen", "", "[CLASS:Edit; INSTANCE:1]")

 

 

Edited by AI123
Link to comment
Share on other sites

Link to comment
Share on other sites

I run SciTe as admin now and set #RequireAdmin after the includes. For your code (I set it after the line $hWnd = WinWait("[CLASS:#32770]","",15)) I get this output:

control = 0x00000000

 

Edited by AI123
Link to comment
Share on other sites

Console ouput for $hWnd:

$hWnd = 0x00030222

Info tool values for the browser file open window:

Property    Value
Title       Öffnen
Class       #32770
Position    18, 10
Size        873, 557
Style       0x96CC02C4
ExStyle     0x00010101
Handle      0x000000000009066C

 

Edited by AI123
Link to comment
Share on other sites

64 bits handle ?  Not the same handle ?  No wonder it doesn't work...

Seems there is more than 1 windows #32770.  Check with WinList.

I remember seeing something similar in the past, but just can remember what was the solution...

Link to comment
Share on other sites

I have a lot of handles

Code:

; https://www.autoitscript.com/autoit3/docs/functions/WinList.htm
; Retrieve a list of window handles.
Local $aList = WinList()

; Loop through the array displaying only visable windows with a title.
For $i = 1 To $aList[0][0]
    If $aList[$i][0] <> "" And BitAND(WinGetState($aList[$i][1]), 2) Then
        ;MsgBox($MB_SYSTEMMODAL, "", "Title: " & $aList[$i][0] & @CRLF & "Handle: " & $aList[$i][1])
        ConsoleWrite("Handle: " & $aList[$i][1] & " Title: " & $aList[$i][0] & @CRLF)
    EndIf
Next

Output (some * to protect private data):

Handle: 0x0003070C Title: (Frozen) AutoIt v3 Window Info
Handle: 0x000A06EC Title: Öffnen
Handle: 0x000F0948 Title: Passwort speichern?
Handle: 0x0007076A Title: Spreadshirt - Google Chrome
Handle: 0x0007081E Title: D:\Dokumen***
Handle: 0x000708D8 Title: D:\Dokumen***
Handle: 0x000D06A0 Title: Can't get handle of Browser File Open dialog - AutoIt General Help and Support - AutoIt Forums - Google Chrome
Handle: 0x000507EE Title: Passwort speichern?
Handle: 0x000B0C62 Title: Spreadshirt - Google Chrome
Handle: 0x00150554 Title: anm***
Handle: 0x000204B2 Title: Fo***
Handle: 0x0003003C Title: Microsoft Text Input Application
Handle: 0x00020156 Title: Program Manager

 

Link to comment
Share on other sites

4 minutes ago, Aelc said:

care with 

[CLASS:#32770]

because all explorer windows and also some other apps have this class...

 

Ok, but it's ok to use the title instead of class number for WinWait ($hWnd = WinWait("Öffnen", "", 15))?

Because this works but could there be problems?

Edited by AI123
Link to comment
Share on other sites

well yes when the title changes while working with it e.g. or you have another title e.g. when you start your script next time

EDIT: also when the title exist 2 times :) 

Edited by Aelc

why do i get garbage when i buy garbage bags? <_<

Link to comment
Share on other sites

13 minutes ago, Aelc said:

well yes when the title changes while working with it e.g. or you have another title e.g. when you start your script next time

 

Now in Windows the title for the browser file open dialog is always Öffnen/Open. For my purpose I can use it until Microsoft changes the title of this window. It would only be problematic if the title would change for each opening or if I change the os language (for example if I search for "Öffnen" if os language is English and title is "Open"). But for now I will use it this way.

Thank you for all your help Aelc and Nine.

Edited by AI123
Link to comment
Share on other sites

generally you never should have opened 2 FileOpenDialog windows at same time...

I'm not really sure if it's even affected by OS language... 

but normally a fileopendialog window pops up so you wouldn't need to use winwait()

try to use winwaitactive() then your ControlClick() should work better :P 

greetings from germany to germany :P 

why do i get garbage when i buy garbage bags? <_<

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...