25

I'm using selenium webdriver, C#.

Is it possible to make work webdriver with Firefox select file dialog? Or must I use something like AutoIt?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Oleg Strokatyy
  • 631
  • 2
  • 10
  • 23

9 Answers9

38

If you are trying to select a file for upload Selenium 2 supports HTML file inputs. For example:

HTML

<input type="file" id="uploadhere" />

Selenium Code

IWebElement element = driver.FindElement(By.Id("uploadhere"));
element.SendKeys("C:\\Some_Folder\\MyFile.txt");

Basically you "type" (with SendKeys) the full file path to the file input element. Selenium handles the file selection dialog for you.

However if you want to manipulate an arbitrary file selection dialog, then like Anders said, you have to go outside of Selenium.

prestomanifesto
  • 12,528
  • 5
  • 34
  • 50
10

No, WebDriver cannot interact with dialogs - this is because dialogs are the domain of the operating system and not the webpage.

I know people that have had luck with autoit as well as the Automation API provided by .Net.

Another option would be to skip the file dialog entirely and issue a POST or a GET, but this requires more advanced knowledge of the website as well as understanding how construct a POST/GET.

You could try Webinator, it is similar to Selenium in the sense that it is powered by WebDriver. It provides file dialog capabilities and I've had great success with it.

Anders
  • 15,227
  • 5
  • 32
  • 42
4

Here is another solution using remotewebdriver, it works like magic and I loved it.

Here is the class I have:

driver.findElementByLinkText("Upload Files").click();
driver.setLogLevel(Level.ALL);
System.out.println(driver.getCurrentUrl());
WebElement element = driver.findElement(By.xpath("//input[@name='file_1']"));
LocalFileDetector detector = new LocalFileDetector();

//Now, give the file path and see the magic :)              
String path = "D://test66T.txt";
File f = detector.getLocalFile(path);
((RemoteWebElement)element).setFileDetector(detector);
element.sendKeys(f.getAbsolutePath());

//now click the button to finish
driver.findElementByXPath("//html/body/div[9]/div[1]/a/span").click(); 
Darren Oster
  • 9,146
  • 10
  • 48
  • 66
Bijoy Meethal
  • 168
  • 11
  • This works perfectly, saves a huge headache needing to deal with having additional software installed on a testing box (such as AutoIT, et. al.) – nerdwaller Jul 09 '14 at 18:18
  • 4
    Is this a Java binding though? Because in C# I cannot find a getLocalFile method under LocalFileDetector https://selenium.googlecode.com/git/docs/api/dotnet/html/AllMembers_T_OpenQA_Selenium_Remote_LocalFileDetector.htm – Pavel Zagalsky Nov 05 '15 at 10:35
2

You asked for using AutoIt for the file dialog. This is easy and you can do it with C#.

  • Install nuget package AutoItX.Net

  • Use the demo code below

  • Change the dialog title string as you need

     public static void InsertIntoFileDialog(string file, int timeout = 10)
     {
         int aiDialogHandle = AutoItX.WinWaitActive("Save As", "", timeout); // adjust string as you need
         if (aiDialogHandle <= 0)
         {
             Assert.Fail("Can't find file dialog.");
         }
         AutoItX.Send(file);
         Thread.Sleep(500);
         AutoItX.Send("{ENTER}");
         Thread.Sleep(500);
     }
    

This helped me after I had trouble with Appium/Selenium related to file dialogs.

Beauty
  • 865
  • 11
  • 14
  • 1
    Thanks for this! I've been racking my brain for 1.5 days trying not to use another package. But I have conceded and this works really well! For anyone reading this, it's selects the active window, so when you're debugging, make sure to add your break points after the .Send() other wise it types your file path in your IDE. – agleno Feb 16 '21 at 14:27
  • In my case, the site's file input was hidden and not accepting key strokes. This solution worked perfectly. – Aron Boyette Jan 10 '22 at 00:45
1

According to Nadim Saker

.Net has a library to handle file upload dialog. It has a SendKeys class that has a method SendWait(string keys). It sends the given key on the active application and waits for the message to be processed. It does not return any value.

Louis
  • 146,715
  • 28
  • 274
  • 320
Vadim
  • 11
  • 1
1

This can be done as follows, tested and working with Internet Explorer and Chrome driver

var allowsDetection = this.Driver as IAllowsFileDetection;
if (allowsDetection != null)
{
   allowsDetection.FileDetector = new LocalFileDetector();
}

Driver.FindElement(By.Id("your-upload-input")).SendKeys(@"C:\PathToYourFile");

Reference https://groups.google.com/forum/#!msg/webdriver/KxmRZ8MkM4M/45CT4ID_WjQJ

lead
  • 91
  • 1
  • 6
DalSoft
  • 10,673
  • 3
  • 42
  • 55
0

If you want to upload a file, and not use the WebDriver, the only solution I've come across is AutoIt. It allows you to write a script and convert it to an executable which you can then call from within your code. I've used it successfully while working with an ActiveX control.

sparkyShorts
  • 630
  • 9
  • 28
  • Using AutoIt as support for problems can be a sweet workaround. There is also a .NET interface. So you can write code for dialog interaction easily in C#. ..... Here you see integration information and an example for sending text (e.g. a path) to a window: https://documentation.help/AutoItX/dotnet_usage.htm ..... AutiIt is also available as nuget package. (Only useful for testing Windows applications, not for mobiles.) – Beauty Nov 18 '20 at 09:07
  • I wrote a demo code for AutoIt in C#. It was quickly done and works well. Have a look to my answer: https://stackoverflow.com/a/64891474/789423 – Beauty Nov 18 '20 at 10:46
0

Another approach is to use System.Windows.Forms.SendKeys.SendWait("pathToFile").
I use it with success everywhere where i cant just send keys to element like described by @prestomanifesto.

Bart Wojtala
  • 1,290
  • 9
  • 7
0

I used this to solve the problem... try it if all above does not works

    Actions action = new Actions(driver);
    action.SendKeys(pObjElement, Keys.Space).Build().Perform();

    Thread.Sleep(TimeSpan.FromSeconds(2));

    var dialogHWnd = FindWindow(null, "Elegir archivos para cargar"); // Here goes the title of the dialog window
    var setFocus = SetForegroundWindow(dialogHWnd);
    if (setFocus)
    {

        Thread.Sleep(TimeSpan.FromSeconds(2));
        System.Windows.Forms.SendKeys.SendWait(pFile);
        System.Windows.Forms.SendKeys.SendWait("{DOWN}");
        System.Windows.Forms.SendKeys.SendWait("{TAB}");
        System.Windows.Forms.SendKeys.SendWait("{TAB}");
        System.Windows.Forms.SendKeys.SendWait("{ENTER}");
    }

    Thread.Sleep(TimeSpan.FromSeconds(2));
}
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
José Carlos
  • 1
  • 1
  • 1