Search code examples
pythonseleniumfile-uploadselenium-webdriverpywinauto

Automating file upload using Selenium and pywinauto


I am trying to automate a file uplad in a form. The form works as follows: - some data insert - click on add attachment button - windows dialogue window appears - select the file - open it

I am using python, Selenium webdriver and pywinauto module.

Similar approach was described here but it only deals with file name and not with path to it.

Sending keys to the element with Selenium is not possible because there is no textbox that would contain the path. I have tried using AutoIT with the followin code:

$hWnd = WinWaitActive("[REGEXPTITLE:Otev.*|Op.*)]")

If WinGetTitle($hWnd) = "Open" then
    Send(".\Gandalf")
    Send("{ENTER}")
Else
    Send(".\Gandalf")
    Send("{ENTER}")
EndIf

The code basically waits for window with title Open or Otevrit (in CZ) to appear and then do the magic. This code is compiled into an .exe and ran at appropriate moment.

The code works fine and does the upload, but I can not alter the path to file. This is neccessary if I want run my code on any computer. The mobility of the code is neccessary because it is a part of a desktop application for running Selenium tests.

The window which I am trying to handle is:

enter image description here

Basically I would like to input my path string and open the file location. After that I would input the file name and open it (perform the upload). Currently my code looks like:

    # click on upload file button:
    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@class=\"qq-upload-   button-selector qq-upload-button\"]"))).click()
    # wait two seconds for dialog to appear:
    time.sleep(2)
    # start the upload
    dialogWindow = pywinauto.application.Application()
    windowHandle = pywinauto.findwindows.find_windows(title=u'Open', class_name='#32770')[0]
    window = dialogWindow.window_(handle=windowHandle)
    # this is the element that I would like to access (not sure)
    toolbar = window.Children()[41]
    # send keys:
    toolbar.TypeKeys("path/to/the/folder/")
    # insert file name:
    window.Edit.SetText("Gandalf.jpg")
    # click on open:
    window["Open"].Click()

I am not sure where my problem is. Input the file name is no problem and I can do it with:

window.Edit.SetText("Gandalf.jpg")

But For some reason I can't do the same with the path element. I have tried setting focus on it and clicking but the code fails.

Thank you for help.

BUTTON HTML:

<div class="qq-upload-button-selector qq-upload-button" style="position: relative; overflow: hidden; direction: ltr;">
        <div>UPLOAD FILE</div>
    <input qq-button-id="8032e5d2-0f73-4b7b-b64a-e125fd2a9aaf" type="file" name="qqfile" style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0; height: 100%;"></div>

Solution

  • Try following code and let me know in case of any issues:

    WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@type="file"][@name="qqfile"]'))).send_keys("/path/to/Gandalf.jpg")
    

    P.S. You should replace string "/path/to/Gandalf.jpg" with actual path to file