2
votes

So I'm trying to upload a file with selenium webbrowser send_keys, but it doesn't work. Please help.

from selenium import webdriver
import  time

driver = webdriver.Chrome(executable_path="../drivers/chromedriver")
driver.implicitly_wait(5)
driver.maximize_window()

driver.get("https://postimages.org/nl/")

element = driver.find_element_by_xpath("//*[@id='uploadFile']")
element.send_keys("Demo/test.png")

error is:

Traceback (most recent call last): File "/home/kuba/PycharmProjects/Test/Demo/UploadTest.py", line 12, in element.send_keys("Demo/test.png") File "/home/kuba/PycharmProjects/Test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 477, in send_keys self._execute(Command.SEND_KEYS_TO_ELEMENT, File "/home/kuba/PycharmProjects/Test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute return self._parent.execute(command, params) File "/home/kuba/PycharmProjects/Test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/home/kuba/PycharmProjects/Test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable (Session info: chrome=84.0.4147.89)

1

1 Answers

0
votes

The root cause of the problem is due to the 'upload file' link on the website (postimages.org) not being an input field.

Selenium webdriver for Python allows file upload through the use of send_keys method only if the upload button is an input field.

Expected (For send_keys to work)

<input id="uploadFile" type="file">

Actually found (reason why send_keys is not working)

<span id="uploadFile" class="btn btn-lg dz-clickable" >

How to fix the problem?

The approach of send_keys will not work for this website, as it does not have the input element required by webdriver. Therefore, an alternative approach need to be used for the purpose.

The website - https://postimages.org/nl/ supports 'copy + paste' functionality for uplaoding images.

This can be done in python as follows:

Step 1: Copy an image to the system clipboard

Step 2: Use Webdriver to click on the web page.

Step 3: Use webdriver to send 'CTRL + V' to web page. Verify that the image gets uploaded.