0

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!

2
  • What is the problem with your tried code?? Is there any exception or something else??CommentedAug 16, 2016 at 16:28
  • It just won't switch to the Windows Upload Dialog
    – Marj
    CommentedAug 17, 2016 at 14:00

3 Answers 3

2

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!

    1

    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 (i.e., 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.

    2
    • I tried to add fileAttachTA.SendKeys(filePath); but it won't get into the Windows Upload Dialog itself.
      – Marj
      CommentedAug 16, 2016 at 18:22
    • 1
      But that's my point. The variable in fileAttachTAisn't an <input type="file"> element. It's an <object> element. So no, SendKeys won't work on it. That's the whole point of my answer.
      – JimEvans
      CommentedAug 17, 2016 at 21:44
    0

    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.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.