Jump to content

I can't activate the "File Open" window on Chrome and Edge


Recommended Posts

Hi, I'm trying to upload a file with a web browser but I can't select the Open File window to select the file that I want to upload.

TL;DR I can't handle the Open File window of Chrome and MS Edge. AutoIt just doesn't find it by its title (in Firefox yes, but not on Chrome and MS Edge).

With another software (Selenium Webdriver in Python) I access to a web page with a common HTML5 file uploader and I click on it, but when the Open File window comes up, I want to select a file and press Enter (write the filepath that I give and press Enter key or Open button).

I have the idea, and I have the script on AutoIt too... but it doesn't work on Windows Edge and Chrome, only works on Firefox and Pale Moon (who are basically the same).

Here is my code:

#include <MsgBoxConstants.au3>
$title = "Abrir" ; My Windows is in spanish, but I guess it's "Open" in English and it's the same code
WinActivate($title)
If WinActive($title) Then
    send("C:\Users\myuser\images\my_image.jpg")
    Send("{ENTER}")
Else
    MsgBox($MB_SYSTEMMODAL, "WinActive", "Window not found.")
EndIf

In Firefox (and Pale Moon) the Open File window comes up, the file path is written and the Enter key is pressed. It works perfect.

In Chrome and Edge, I never see the Open File window on top... and I neither see "Window not found" message (seems like AutoIt finds it but doesn't send keys).

Edit: before to send this post, I started thinking again and... after several tests, I discovered one thing: when you select the window, in Firefox (and Pale Moon) the cursor goes to the combobox (where I have to put the text) and it's possible to write just after clicking (or opening, what in AutoIt it is "Activate" I guess) the window but in Chrome and Edge it isn't like that, when you click the window, the cursor doesn't go to the combobox.

With that "discovery", I tried to select the combobox but... I failed again. And now I have no idea how to go ahead.

#include <MsgBoxConstants.au3>
$title = "Abrir"
WinActivate($title)
If WinActive($title) Then
    ControlClick($title, "", 1148) ; Still it does not work
    send("C:\Users\myuser\images\my_image.jpg")
    Send("{ENTER}")
Else
    MsgBox($MB_SYSTEMMODAL, "WinActive", "Window not found.")
EndIf

 

Edited by Rhazz
Link to comment
Share on other sites

On 2/8/2016 at 7:37 AM, PACaleala said:

My cat asked :" Did Rhazz try Control - o " ?

Sorry mate, but I don't understand.

Control+O opens the "File Open" window... but I'm asking about how to use that window on Chrome and Edge, where at the moment I can't find this window (I mean handle with AutoIt), to write the file directory and use the open button.

AutoIt just doesn't find the window by its title, and so I can't use it.

Link to comment
Share on other sites

Hello. Hola

 

Probably something like this could do the trick.

 

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Process.au3>
#include <Misc.au3>
#include <WinAPIProc.au3>
#include <Array.au3>
Global $hDLL, $hWinEventProc, $hHook

HotKeySet("{ESC}", "_HotKey") ; Exit


MsgBox(0,"How To Use:","Just Open A Chrome Tab Press CTRL+O And you will get a File Loaded",5)

Global Const $sFile=@DesktopDir & "\DemoFile.txt"
FileWrite($sFile,"Hola Soy Danyfirex")

Global Const $EVENT_SYSTEM_DIALOGSTART = 0x0010 ;An MSAA event indicating that a dialog box was displayed.

Global $EVENT_Min = $EVENT_SYSTEM_DIALOGSTART
Global $EVENT_Max = $EVENT_SYSTEM_DIALOGSTART

$hDLL = DllOpen("User32.dll")
$hWinEventProc = DllCallbackRegister("_WinEventProc", "none", "hwnd;int;hwnd;long;long;int;int")
If Not @error Then
    OnAutoItExitRegister("OnAutoItExit")
Else
    MsgBox(16 + 262144, "Error", "DllCallbackRegister(_WinEventProc) did not succeed.")
    Exit
EndIf


$hHook = _SetWinEventHook($EVENT_Min, $EVENT_Max, $hDLL)


While 1
    Sleep(10)
WEnd



Func _WinEventProc($hHook, $iEvent, $hWnd, $idObject, $idChild, $iEventThread, $iEventTime)
    Local $PID = WinGetProcess($hWnd)
    Local $sEventProcName = _ProcessGetName($PID)

    If StringInStr(WinGetTitle($hWnd), "abrir") And $sEventProcName="chrome.exe" Then
        MsgBox(0,"Information","Google Chrome Dialog Detected" & @CRLF & _
        "Load A File For testing..." & @CRLF,3)
         ControlSetText($hWnd,"","Edit1",$sFile)
         ControlClick($hWnd,"","Button1")
    EndIf

EndFunc   ;==>_WinEventProc

Func _SetWinEventHook($iEventMin, $iEventMax, $hDLLUser32)

    Local $aRet=0
    Local Const $WINEVENT_OUTOFCONTEXT = 0x0
    Local Const $WINEVENT_SKIPOWNPROCESS = 0x2
    If Not $hDLLUser32 Or $hDLLUser32 = -1 Then $hDLLUser32 = "User32.dll"
    $aRet = DllCall($hDLLUser32, "hwnd", "SetWinEventHook", _
            "uint", $iEventMin, _
            "uint", $iEventMax, _
            "hwnd", 0, _
            "ptr", DllCallbackGetPtr($hWinEventProc), _
            "int", 0, _
            "int", 0, _
            "uint", BitOR($WINEVENT_OUTOFCONTEXT, $WINEVENT_SKIPOWNPROCESS))
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc   ;==>_SetWinEventHook

Func _HotKey()
    Switch @HotKeyPressed
        Case "{ESC}"
            Exit
    EndSwitch
EndFunc   ;==>_HotKey

Func OnAutoItExit()
    If $hWinEventProc Then
        DllCallbackFree($hWinEventProc)
    EndIf
    If $hHook Then DllCall("User32.dll", "int", "UnhookWinEvent", "hwnd", $hHook)
    If $hDLL Then DllClose($hDLL)
EndFunc   ;==>OnAutoItExit

 

Edit: This is for chrome. If you want to handle MS Edge Check line 45.

Saludos

Edited by Danyfirex
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

×
×
  • Create New...