Home > Back-end >  Selenium : Add multiple path with send_keys
Selenium : Add multiple path with send_keys

Time:12-02

When I try to add multiple path to an input type=file, one per one, it adds the file 1 then the file 1 and 2 etc...

new_images = modify_images(ad['images_url'])
time.sleep(4)
add_images = driver.find_element(By.XPATH, image_add_xpath)
time.sleep(2)
for i in range(len(new_images)):
    print("image "   str(index)   " "   new_images[i])
    time.sleep(3)
    url = r"C:\\Users\\33651\Documents\Documents\\facebook-automate\\facebook-automate\\" new_images[i]
    print(url)
    print("uploading")
    add_images.send_keys(url)
    print("Image uploadé")
    time.sleep(2)

I already tried to execute only one send_keys with the concatenation of the multiple path with the " \n " separator. It doesn't work. I also tried to clear the input but It doesn't work neither.

add_images = driver.find_element(By.XPATH, image_add_xpath)
        time.sleep(2)
        all_path_images = ""
        for i in range(len(new_images)):
            all_path_images  = new_images[i]   ' \n '
            
        add_images.send_keys(all_path_images)
        print("Image uploadé")
        time.sleep(2)

CodePudding user response:

From your code trials shown here looks like you didn't add the new line correctly.
To upload multiple files you can construct a string adding all the absolute paths of the uploaded files separated by \n , as following:

all_path_images = ""
for i in range(len(new_images)):
    all_path_images   = r"C:\\Users\\33651\Documents\Documents\\facebook-automate\\facebook-automate\\"   new_images[i]   ' \n '
#remove the last new line character from the string
all_path_images = all_path_images.strip()
add_images.send_keys(path)
  • Related