3

I am working on a browser automation project in Python using selenium. I am trying to upload a picture to a page. I login, go to the page, and click the upload button. After clicking the upload button, a windows file browser opens up, where I have to select the file path and hit the open button on the windows browser. I am looking to automate this process. Following is an image of whats going on, to clarify:

enter image description here

Now, I want to give the file path and click the open button. To click the "Upload Photos" button, I use the following line of code:

browser.find_element_by_css_selector("a._3m1z").click()

I searched the internet, and came across the send_keys function. I tried the following:

browser.find_element_by_css_selector("a._3m1z").send_keys(os.getcwd()+"/image.png")

I get the following errors:

  File "C:\Users\Umar\AppData\Local\Programs\Python\Python37\lib\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=79.0.3945.88)

Can someone point what is going wrong and point me in the right direction? I am using python 3.7

2 Answers 2

1

Selenium can only interact with the browser. The window that appears after clicking the "Upload Photos" button is the Windows File Explorer, unrelated to the browser. You'll have to use a tool like PyWinAuto or AutoIt to interact with the File Explorer.

In regards to what you're trying to accomplish, there are some workarounds: (1) using APIs, (2) passing in the file path directly to the upload button, etc. Depending on what the purpose of your script is (Automated UI Testing, automating some task of yours, etc.), some of these alternatives are better than others.

Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to use the (2) option (sending path to the webelement). Its giving me the error element not interactable.
I have the html for this button in the question in the link below: stackoverflow.com/questions/59550882/…
-1

Here's an idea for doing a file upload without popping the chooser:

filename = 'x.jpg'
with open(filename, "rb") as file:
  content = base64.b64encode(file.read()).decode('utf8')
mimeType = "image/jpeg"
selector = "input[type=file]"

driver.execute_async_script("""
  const [filename, content, mimeType, selector, cb] = arguments

  const dt = new DataTransfer()
  const response = await fetch(`--data:${mimeType};base64,${content}`)
  const file = new File([await response.blob()], filename)
  dt.items.add(file)

  const element = document.querySelector(selector)
  element.files = dt.files
  element.dispatchEvent(new Event('input', { bubbles: true }))
  cb()
""", filename, content, mimeType, selector)

driver.find_element_by_css_selector('input[type=submit]').click()

Also you might want to think about switching to Puppeteer for things like this because Selenium is not likely to ever have a good implementation of this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.