Automate file upload using Selenium and ng-file-upload Automate file upload using Selenium and ng-file-upload selenium selenium

Automate file upload using Selenium and ng-file-upload


There has to be a file input hidden which is implicitly responsible for the file upload. For instance, the angular-file-upload DEMO page has it hidden at the bottom of a page.

Here is a working example:

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get("https://angular-file-upload.appspot.com/")# wait for the upload box to be visiblewait = WebDriverWait(driver, 10)element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[ngf-select]")))# get a modelmodel = element.get_attribute("ng-model")# find a file input by modelfile_input = driver.find_element_by_css_selector('input[ngf-select][ng-model="%s"]' % model)# upload an imagefile_input.send_keys("/absolute/path/to/dr-evil-and-minion-laughing.png")

Results into:

enter image description here