codyc4321 codyc4321 - 1 month ago 16
Python Question

upload files with selenium webdriver python

I'd like to use webdriver to pick a file but following other answers they do not work. They say just give the button a filepath, and this doesn't do anything. The upload looks like this after clicking the button:

enter image description here

This is what others say to do but doesn't work:

element = driver.find_element_by_name("file")
element.send_keys("/home/pavel/Desktop/949IH3GNHAo.jpg")


How can I submit files once I'm in a webdriver instance? Thank you

Answer

This problem solved at uploading photos to Craigslist with Python and Selenium

def add_photo(self, filepath_to_photo):
    add_images_button = self.driver.find_element_by_name('upload')
    add_images_button.click()
    photo_filepath_input_box = self.driver.find_element_by_xpath("//input[@type='file']")
    photo_filepath_input_box.send_keys(filepath_to_photo)