简体   繁体   中英

File upload using SendKeys in Selenium not working

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

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']"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM