Home > Mobile >  File upload using SendKeys in Selenium not working
File upload using SendKeys in Selenium not working

Time:08-20

I have following code

IWebElement chooseFile = driver.FindElement(By.XPath("//button[@id='btnSelect']"));
chooseFile.SendKeys(@"D:\Workspace\Base Version (Do Not Use)\file.tar.gz");

Its opening up File Explorer but not uploading the file. I tried using JavaScript Executer as below

String filePath = "D:\\Workspace\\Base Version (Do Not Use)\\5GGW3-OMNI-1_R220200BhaT0301E0266.tar.gz";
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("document.getElementById('btnSelect').value='"   filePath   "';");

didn't work either. Does anyone know of any solution? I'm using FirefoxDriver in C# .NET

CodePudding user response:

You are trying to send a file to a wrong element.
A web element actually receiving uploaded file on a web page is normally matching this XPath: //input[@type='file'].
It is not a visible element.
So, instead of

IWebElement chooseFile = driver.FindElement(By.XPath("//button[@id='btnSelect']"));

Try using

IWebElement chooseFile = driver.FindElement(By.XPath("//input[@type='file']"));
  • Related