Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Windows Save/Open Open Dialogue

Tags:

selenium

I am trying to use selenium webdriver to automate web application. So say the dialog below pops up, my goal is to automatically click Save File, and then hit Ok.

Windows Save/Open File DownloadDialouge

like image 555
Jag Avatar asked Oct 21 '22 14:10

Jag


1 Answers

WebDriver cannot directly interact with dialog windows this is because dialog windows are the domain of the operating system and not the webpage. However its possible to do actions on dialog windows using SendKeys class method SendWait() of name space System.Windows.Forms

using System.Windows.Forms;

In the example code below a PLUpload button is pressed, which opens a Windows Dialog to select the file to upload.

Following lines are used to send key values to the dialog window displayed.

SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock.jpg");
SendKeys.SendWait(@"{Enter}");

Detailed reference of SendKeys class in C# can be found in http://msdn.microsoft.com/en-au/library/system.windows.forms.sendkeys.aspx

using System;
using System.Windows.Forms;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Interactions;
using NUnit.Framework;
namespace BusinessCreation
{
    class PlUpload
    {
        static void Main(string[] args)
        {
               IWebDriver driver = new FirefoxDriver();
               driver.Navigate().GoToUrl("http://www.plupload.com/example_queuew               idget.php");
               driver.FindElement(By.XPath("//object[@data='/plupload/js/pluploa               d.flash.swf']")).Click();
               SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock               .jpg");
               SendKeys.SendWait(@"{Enter}");
         }
    }
}
like image 50
CheryJose Avatar answered Oct 25 '22 19:10

CheryJose