Search code examples
pythonseleniumfileselenium-webdriverupload

How to upload multiple files to a website using Python and Selenium?


Trying to upload multiple files to a website using Python and Selenium but only the first file is picked up and not the others, any idea why? They are actually PGP files but i dont think that matters much

Not sure whats wrong with my script, im using Selenium and Glob as the main source of uploading and file manipulation. I have all my files set for variable and i also used the send.keys accurately, not sure why its only picking up the first instance of every file and not all of them.

For Example, the directory folder that all of these are pointing too might have 2 or 3 of the "ack" file. When i execute this code it will only pick up the first one and miss the other 2.

    # this imports all the packages that we need to run our script
import glob
import datetime
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


# this creates the format for the name convention of today
today = datetime.datetime.today().strftime("%Y%m%d")


# these are the files that will be uploaded to Chase
print("The following files have been found and uploaded to Chase:")

b3input = glob.glob(r'S:\Chase\up\b3pinput_MOORCL_' + today + '*.pgp')
pmntr = glob.glob(r'S:\Chase\up\b3pmntr_MOORCL_' + today + '*.pgp')
ack = glob.glob(r'S:\Chase\up\ack_b3ptran_LIT_MOORCL_' + today + '*.pgp')

# the following file variable is for redundancy (to pick up any file that is a PGP File)
for pgpFiles in glob.glob(r'S:\Chase\up\*.pgp'):
    print(pgpFiles)



# this gives the location of our chrome driver
driver_path = r'C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driver_path)



# this tells the driver to go to the following website
driver.get('https://website.com')

delay = 10 # seconds

try:
    # Waits for 10 seconds looking for the user name text area
    login_box = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.ID, "userIDInput"))
    )
    login_box.send_keys('username')

    # Waits for 10 seconds looking for the password text area
    password_box = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.ID, "passwordInput"))
    )
    password_box.send_keys('password')

    login_button = driver.find_element_by_id('loginButon')
    login_button.click()

    chase_ToJPM = driver.get('https://website.com/To_JPMC')

    #upload_files = driver.find_element_by_id('allFiles_actions::upload')
    #upload_files.click()

    upload_files = WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.ID, "allFiles_actions::upload"))
    )

    #upload_files.send_keys(b3input)
    #upload_files.send_keys(pmntr)
    #upload_files.send_keys(ack)
    upload_files.send_keys(pgpFiles)

    time.sleep(7)

    driver.quit()

  
except TimeoutError:
    print("Loading took too much time!")

Solution

  • You can upload multiple files using the following line of code:

    uploadElement.send_keys("S:\Chase\up\abc.pgp \n S:\Chase\up\pqr.pgp \n S:\Chase\up\xyz.pgp")
    

    References

    You can find a couple of relevant detailed documentations in: