title: Automating file upload using Selenium and pywinauto
body: <p>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</p>
  
  <p><strong>I am using python, Selenium webdriver and pywinauto module.</strong></p>
  
  <p>Similar approach was described <a href="https://stackoverflow.com/questions/21236183/upload-a-file-using-webdriver-pywinauto">here</a> but it only deals with file name and not with path to it.</p>
  
  <p>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:</p>
  
  <pre><code>$hWnd = WinWaitActive("[REGEXPTITLE:Otev.*|Op.*)]")
  
  If WinGetTitle($hWnd) = "Open" then
      Send(".\Gandalf")
      Send("{ENTER}")
  Else
      Send(".\Gandalf")
      Send("{ENTER}")
  EndIf
  </code></pre>
  
  <p>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.</p>
  
  <p>The code works fine and does the upload, but I <strong>can not alter the path to file</strong>. 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. </p>
  
  <p>The window which I am trying to handle is:</p>
  
  <p><a href="https://i.stack.imgur.com/GHmcW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GHmcW.png" alt="enter image description here"></a></p>
  
  <p>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:</p>
  
  <pre><code>    # 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()
  </code></pre>
  
  <p>I am not sure where my problem is. Input the file name is no problem and I can do it with:</p>
  
  <pre><code>window.Edit.SetText("Gandalf.jpg")
  </code></pre>
  
  <p>But For some reason I can't do the same with the path element. <strong>I have tried setting focus on it and clicking</strong> but the code fails.</p>
  
  <p>Thank you for help.</p>
  
  <p><strong>BUTTON HTML:</strong></p>
  
  <pre><code>&lt;div class="qq-upload-button-selector qq-upload-button" style="position: relative; overflow: hidden; direction: ltr;"&gt;
          &lt;div&gt;UPLOAD FILE&lt;/div&gt;
      &lt;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%;"&gt;&lt;/div&gt;
  </code></pre>