0

My automation script needs to upload an image to the webpage but I can't seem to upload the image using the normal send_keys method. I suspect it has something to do with the Angular components but I'm not sure how to access them using Selenium webdriver.

I have tried the following: Automate file upload using Selenium and ng-file-upload and How to upload file using python+selenium?

These don't seem to have the solution I'm looking for.

<button class="md-raised choose-file md-button md-ink-ripple ng-empty ng-valid" type="button" ng-transclude="" ngf-select="" accept="image/*" ng-model="vm.uploader.original" aria-invalid="false">Choose file</button>

I don't have any errors because I can successfully locate the element but the image is not uploaded/sent.

file_input.send_keys("/location/of/image/profile_student.jpg")
vezunchik
  • 3,669
  • 3
  • 16
  • 25
Leroy Sharp
  • 55
  • 3
  • 10
  • Do you have any input field beside the `Choose file` button? Or you are clicking on `Choose file` button and its pop up windows form? – KunduK May 16 '19 at 10:30
  • Possible duplicate of [selenium file upload without element](https://stackoverflow.com/questions/51888388/selenium-file-upload-without-input-type-file-element) – Ardesco May 16 '19 at 11:12
  • @KunduK no, I only have the choose file button. Once you click it, it opens the "open file" pop up box. – Leroy Sharp May 16 '19 at 13:05
  • Ok.To access windows object you need to use autoit with selenium. – KunduK May 16 '19 at 13:11

1 Answers1

2

To handle windows object you can use autoit However there is Python binding for AutoItX3.dll

You need to install PyAutoIt using pip.

pip install -U pyautoit

You need to import autoit on your python scripts.

import autoit

Click on the chose file button first.

driver.find_element_by_xpath("//button[@type='button'][contains(.,'Choose file')]").click()

Then add the following code with the relevant file path

filepath="C:\\filelocation\\filename.jpg"
autoit.win_wait_active("File Upload",5)
if autoit.win_exists("File Upload"):
   autoit.control_send("File Upload","Edit1",filepath+"{ENTER}")

Let me know if you need further assistance.

KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Will this work on mac? It will only be used in a mac/linux environment. – Leroy Sharp May 16 '19 at 13:50
  • I don't use mac or Linux.However worth give it a try.let me know how it goes? – KunduK May 16 '19 at 13:53
  • The installation failed and after doing some research it seems like it only works on Windows. – Leroy Sharp May 16 '19 at 14:16
  • @LeroySharp : just check are you able to install `pip install pypiwin32` this? – KunduK May 16 '19 at 14:51
  • 1
    Using AutoIt is a hack, selenium already supports file uploads, you just need to find the element and populate that. If one doesn't exist you need to hook into the JavaScript implementation using a JavascriptExecutor. You should never use AutoIt, it's brittle, not cross platform compliant and only works on the local machine. – Ardesco May 17 '19 at 13:02