How to upload files using Selenium Webdriver in Java

Asked

Viewed 1,973 times

2

Can someone help me to upload a file using Selenium using Java code?

I made the code below:

firefoxDriver.findElement(By.xpath("//a[@ng-model='picFile']"));
aP.selecionarArquivo(dir+"C:\\Users\\Imagens\\tela.jpg");

public void selecionarArquivo(String path) throws InterruptedException 
{

    WebElement fileInput = firefoxDriver.findElement(By.xpath("//input[@type='file']"));
    fileInput.sendKeys(path);
}

It shows no error, but does not attach the file. It even fills the file name field as if it had attached, but does not enable(The button is only enabled when attaching a file) the Confirm button.

  • I found my problem. It was the file path that was wrong. I thought that when putting a wrong path presented some error message.

1 answer

0

I upload it this way.
If you’ve got a way to do it using your method, even better!

//UPLOAD ARQUIVO
        navegador.findElement(By.xpath("//*[@id=\"ctl00_ctl00_placeHolderMain_mainWebCad_upArquivofile0\"]"))
                .sendKeys("C:"+ File.separator+"Users"+File.separator+"servflex050"
                        +File.separator+"Desktop"+File.separator+"2 - Ciclo 2_Roteiro de Teste"
                        +File.separator+"Arquivos_Importacao"+File.separator+"BRTPREV_ARQUIVO_REPASSE_072019_v2.txt");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.