Search code examples
pythonfile-browser

How to travel to specific file path when Chrome browser asks for a file without using Selenium(third party libraries)


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?


Solution

  • 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()