简体   繁体   中英

How to handle upload of file using Windows Upload Dialog in Selenium

I am trying to upload an attachment using Selenium (C#).

Upon checking the DOM of the site, I noticed that the link to attach files is using object tags . Below is the HTML excerpt:

<object id="ctl00_mainContent_rauFilessilverlight03" class="ruObject" height="22px" type="application/x-silverlight-2" data="data:application/x-silverlight-2," style="width: 100%;"> 
 <param value="/App/somelongjunkyparameters" name="source"/> 
 <param value="true" name="windowless"/> <param value="transparent" name="background"/> 
 <param value="some number" name="minRuntimeVersion"/> 
 <param value="PostData=anotherlongjunkyparameters,SilverlightRowId=ctl00_mainContent_rauFilessilverlight03,AsyncUploadId=ctl00_mainContent_rauFiles,MultipleSelection=Disabled,AllowedFileExtensions=,ServiceHandlerUrl=/App/Telerik.Web.UI.WebResource type=rau,MaxFileSize=0" name="InitParams"/> 
 <param value="true" name="autoUpgrade"/> 
</object>

I have tried this so far:

IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[@class='ruObject']"));
fileAttachTA.Click();
String filePath = "C:/User/My Documents/file.txt";

Selenium was able to find the object, but, should I switch to the Windows Upload Dialog? Hoping to hear from anyone who has experience in this.

Thanks!

I got it and what I did was this:

 IWebElement fileAttachTA = driver.FindElement(By.XPath("//object[@class='ruObject']"));
     fileAttachTA.Click();

     //Switch into the windows upload dialog
     SendKeys.SendWait("^a");
     Thread.Sleep(1000);
     SendKeys.SendWait(file);
     Thread.Sleep(1000);
     SendKeys.SendWait(@"{Enter}");
     Thread.Sleep(1000);

I used the System.Windows.Forms to get the SendKeys.SendWait to work. Thanks everyone!

Whoever developed the website is using a non-standard mechanism for uploading files. Looking at the HTML you provided, it looks like a Silverlight control of some sort. While Selenium WebDriver can properly handle the file selection dialog for uploading a file when a page is using a standard HTML upload mechanism (ie, an <input type="file"> element), it has no hope of doing so with a non-standard upload mechanism. You'll need to find a way to handle the dialog outside of Selenium.

I've had issues with talking to a Windows dialog box when downloading/uploading files. My solution was to utilize user32.dll GetForegroundWindow(). Then created some wait methods for the dialogue box to appear disappear based on header text(still using user32.dll). Then finally created an action to BeginInvoke, waited for the window to pop up, and proceeded with send keys. Don't have code examples in front of me right now, but Google user32.dll Selenium and you'll get some info.

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