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") 
4
  • 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
    CommentedMay 16, 2019 at 10:30
  • Possible duplicate of selenium file upload without <input type="file"> element
    – Ardesco
    CommentedMay 16, 2019 at 11:12
  • @KunduK no, I only have the choose file button. Once you click it, it opens the "open file" pop up box.CommentedMay 16, 2019 at 13:05
  • Ok.To access windows object you need to use autoit with selenium.
    – KunduK
    CommentedMay 16, 2019 at 13:11

1 Answer 1

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.

5
  • Will this work on mac? It will only be used in a mac/linux environment.CommentedMay 16, 2019 at 13:50
  • I don't use mac or Linux.However worth give it a try.let me know how it goes?
    – KunduK
    CommentedMay 16, 2019 at 13:53
  • The installation failed and after doing some research it seems like it only works on Windows.CommentedMay 16, 2019 at 14:16
  • @LeroySharp : just check are you able to install pip install pypiwin32 this?
    – KunduK
    CommentedMay 16, 2019 at 14:51
  • 1
    Using AutoIt is a hack, selenium already supports file uploads, you just need to find the <input type="file"> 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
    CommentedMay 17, 2019 at 13:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.