How to send file within input tag using Selenium and Python

53 Views Asked by At

How do I send a file link to load on the page using selenium?

<input type="file" id="filArquivo" class="infraInputFile" name="filArquivo" size="50" onchange="objUpload.executar();" tabindex="1000">

Does not work:

WebDriverWait(navegador, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="filArquivo"]'))).send_keys("Doc.pdf")

Does not work:

WebDriverWait(navegador, 20).until(EC.element_to_be_clickable((By.ID, 'filArquivo'))).send_keys("Doc.pdf")

Snapshot:

enter image description here

2

There are 2 best solutions below

0
On

Try:

  • Using CSS_SELECTOR:

    WebDriverWait(navegador, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.infraInputFile[name='filArquivo']"))).send_keys("/path/to/Doc.pdf")
    
  • Using XPATH:

    WebDriverWait(navegador, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='infraInputFile' and @name='filArquivo']"))).send_keys("/path/to/Doc.pdf")
    
1
On

Try to use full path to file (os.path.abspath)