0

me here trying to find out how to achieve the next:

I am actually using Selenium to drive trough webpages but when I want to upload some images in any webpage the "Open"(Abrir because in spanish) file browser opens up and I want to know how to browse to the directory I want and select the file(s) that I want to upload --> For example: I go to imgBB URL and I click the START UPLOADING button with Selenium but then I want to browse to an specific folder and then select some files I want, but without using Selenium. enter image description here

Do someone knows what other libraries may I use to select the specific files that I want to upload?

RedEye
  • 205
  • 1
  • 2
  • 6

2 Answers2

2

Solution using pywinauto

This could be done with pywinauto

Disclaimer: I am not pywinauto guru.

import pywinauto

# A handle of the opened dialog.
# "Open" is a title of the dialog window.
handle = pywinauto.findwindows.find_window(best_match="Open")
# An application that the dialog belongs to.
app = pywinauto.application.Application().connect(handle=handle)

# The dialog window itself.
open_dialog = app.window(handle=handle)

# Set the file path.
# It is not necessary to navigate to a folder, you can set an absolute file path instead.
# If you want to add multiple files you have to put each path in double quotes and separate paths with space.

# There is a little magic in this line:
# the "FileName" part depends on the text that is shown right before the input field for paths.
# In your case this should work "NombreDeArchivoEdit"
# open_dialog.NombreDeArchivoEdit.set_edit_text(r'"C:\Users\USERNAME\Pictures\img1.png" "C:\Users\USERNAME\Pictures\img1.png"')
open_dialog.FileNameEdit.set_edit_text(r'"C:\Users\USERNAME\Pictures\img1.png" "C:\Users\USERNAME\Pictures\img1.png"')

# Click the "Open" button. Same magic here.
open_dialog.Open.Click()
# Should work for you.
# open_dialog.Abrir.Click()

enter image description here

Solution with Selenium

  1. To upload files you need to find an input element with type=file that is called when the "Start uploading" button clicked

    upload = driver.find_element(By.ID,"anywhere-upload-input")

  2. And then add a file you want to upload upload.send_keys("C:\\Users\\USERNAME\\\Pictures\\IMAGE.png")

After this, the next step window will be shown.

If you want to upload multiple files you have to combine all file paths into one string and separate them with \n:

upload.send_keys("C:\\Users\\USERNAME\\Pictures\\IMAGE.png \n C:\\Users\\USERNAME\\Pictures\\IMAGE2.png")

Here is the full code:

import time

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service("path/to/chrome/driver")
driver = webdriver.Chrome(service=service)
driver.get("https://imgbb.com/")
upload = driver.find_element(By.ID,"anywhere-upload-input")
upload.send_keys("C:\\Users\\USERNAME\\Pictures\\IMAGE1.png \n C:\\Users\\USERNAME\\Pictures\\IMAGE2.png")

time.sleep(10)

driver.quit()
elebur
  • 465
  • 1
  • 5
  • It is useful anyways but I am looking for a script for a general solution whenever this Open file browser appears. Thanks for sharing this code, I am sure I will use this in other codes – RedEye Jul 15 '23 at 22:25
  • 1
    @RedEye, I got curious about this problem and decided to get a little deeper. After some searching, think I found the solution, see the updated answer. – elebur Jul 16 '23 at 16:50
  • That's a really nice answer @elebur, I really want to give it a try maybe in the following days when I get free time in the week and I will get back to this post to tell about the results. This surely is gonna help a bunch of beginner in this field. Thank you buddy – RedEye Jul 17 '23 at 02:51
  • 1
    Well, I am back and with good news. The answer given by @elebur works like a charm it is the exact result that I wanted. I really appreciate the effort on explaining the code to get a super clear explanation of what's going on. Thanks!!! – RedEye Jul 18 '23 at 01:27
  • You are welcome, @RedEye! Glad I could help. – elebur Jul 18 '23 at 14:59
0

Today I found another useful approach from this gentlemen into making a generic solution and even showing a solution with Selenium altough is a litte bit old answer. Here is the link to the alternative solution using native Python libraries

RedEye
  • 205
  • 1
  • 2
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '23 at 12:22