1

I want to automate uploading a file to this website (it's not my website) but this website is created in some js framework (I think it's react). Now I have a problem uploading file, everything I have tried it's not working. I use Linux (distribution Manjaro) and I'm unable to use AutoIT.

This is what I tried.

file_image = 'image.jpg'

#this
uplod_image = browser.find_element_by_xpath('//input[@qa-id="selectFile"]').send_keys(file_image)

#and this
upload_image = browser.find_element_by_class_name("fileUploadBtn_dropzoneElement_38Gmm").send_keys(file_image)

This is inspected code, main problem is that upload is done like div

enter image description here

I usually got this error...

selenium.common.exceptions.ElementNotInteractableException: Message: Element <div class="fileUploadBtn_dropzoneElement_38Gmm"> is not reachable by keyboard
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
ElementNotInteractableError@chrome://remote/content/shared/webdriver/Errors.jsm:293:5
webdriverSendKeysToElement@chrome://remote/content/marionette/interaction.js:624:13
interaction.sendKeysToElement@chrome://remote/content/marionette/interaction.js:600:11
sendKeysToElement@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:497:24
receiveMessage@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:151:31

Any idea how to solve this problem?

pergunt0
  • 83
  • 7
  • Does this answer your question? [How to upload file ( picture ) with selenium, python](https://stackoverflow.com/questions/8665072/how-to-upload-file-picture-with-selenium-python) – Nick ODell Feb 23 '22 at 22:25

2 Answers2

4

If you check the input element the style attribute present as display: none; That's the reason it is not interacting even using the valid locator.

You need the change the style of the element to display: block; and then try to upload the file using send_keys()

Use java script executor to change the style.

fileupload=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div[qa-id='dropZone']>input[type='file']")))
driver.execute_script("arguments[0].style.display = 'block';",fileupload)
fileupload.send_keys(path/to/file)

Hope this code will works for you.

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • If anyone is trying this in some angular components it may be possible that just changing the style of the input is not working. You can first send a click to the element that opens the user dialog and then a listner is activated and you will be able to use the method described here. – nck Nov 25 '22 at 08:52
1

Normally uploading a file with Selenium is done by:

browser.find_element_by_xpath('//input[@type="file"]').send_keys(file_location)

where file_location is actual absolute path to the file on your local PC.
Like C:/path_to_file/image.jpg

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • I tried something like that, but not working `upload_image = browser.find_element_by_xpath('//input[@qa-id="selectFile"]').send_keys(file_image)` – pergunt0 Feb 23 '22 at 22:53
  • This will not work. The element actually accepting the uploading file is not the element you are clicking on as a user. – Prophet Feb 23 '22 at 22:55
  • 1
    Try the locator I mentioned – Prophet Feb 23 '22 at 22:55