Handling a File Browse Window From Selenium Tests...

118 views
Skip to first unread message

mawa316

unread,
Aug 26, 2016, 3:59:10 PM8/26/16
to Selenium Users
I found something a few days ago that seemed to help me out a bit with testing file upload functionality.  When I click a Browse button in our web app the Windows file browse appears.

To handle, I added a System.Windows.Forms reference and was able to use SendKeys.SendWait.  I didn't know how to leverage this for multi file select so I just looped through an array of files to grab.  So I would click the browse, send the path to the file then send Enter.

I also saw that a sleep timeout might be needed, which appeared to be my case, so I added...

SendKeys.SendWait(file path)
Thread.Sleep(2000)
SendKeys.SendWait(enter)

This appears to work for the most part, but at times a file is skipped or it gets and just hangs with the file browse window open.  I really can't tell what is causing it.  So, I thought I would just or could hopefully somehow multi select the files and get them all in one trip instead of looping through the array of files.

Does anyone know how I can accomplish this or maybe pinpoint why my loop isn't always reliable.

Any help, as always, appreciated!!

mawa316 .

unread,
Aug 30, 2016, 6:26:03 PM8/30/16
to seleniu...@googlegroups.com
Here's the weak workaround I came up with...

Send the path to the files first
Send enter
Run through a for loop to press tab 9 times.  That gets me into the listed files.
Send a Control A
Send enter and that seems to do the trick.

In the meantime what I did was switched to this, which seems to be holding up...

Thread.Sleep(2000)
SendKeys.SendWait(file path)
Thread.Sleep(2000)
SendKeys.SendWait(enter)


--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/zaL_w5enGGI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/fd55b12a-f6c9-43da-be48-9dd05caa2a8d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mawa316 .

unread,
Sep 1, 2016, 7:50:11 PM9/1/16
to seleniu...@googlegroups.com
Is there any way to handle the Windows File Browse dialog with some type of java script and the Selenium java script executor?  Both methods I've described above give inconsistent results.  Both will be successful for several successive runs then they stall during File Browse handling.

To post to this group, send email to seleniu...@googlegroups.com.

Jim Evans

unread,
Sep 1, 2016, 9:00:24 PM9/1/16
to Selenium Users
I'd venture to say that one reason you're getting little response here is that you're entirely bypassing the WebDriver mechanism for uploading files. The method for uploading files is to use the SendKeys method on the WebElement interface. Of course, that mechanism works only on <input type="file"> elements, and many web developers attempt to replace that mechanism because they incorrectly think they have to.

mawa316 .

unread,
Sep 2, 2016, 9:28:14 AM9/2/16
to seleniu...@googlegroups.com
The way I've gone the route I have is because the browse window that pops up is a Windows based window and not from our web app.

In hunting and pecking around I did see an input type="file" example and it was a browse button that brought up the same Windows File Browse dialog I'm seeing.  So, maybe you're right and SendKeys will work.

I'll inspect the particular element more and see if I can improve upon the test.  Thanks for setting me straight!  You may regret that as I'm sure I'll have more questions!  :-D

On Thu, Sep 1, 2016 at 9:00 PM, Jim Evans <james.h....@gmail.com> wrote:
I'd venture to say that one reason you're getting little response here is that you're entirely bypassing the WebDriver mechanism for uploading files. The method for uploading files is to use the SendKeys method on the WebElement interface. Of course, that mechanism works only on <input type="file"> elements, and many web developers attempt to replace that mechanism because they incorrectly think they have to.
--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/zaL_w5enGGI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.

mawa316 .

unread,
Sep 2, 2016, 9:37:02 AM9/2/16
to seleniu...@googlegroups.com
Ooops, I'll play around but one quick question.  I'm guessing I have to click the browse button first then send keys w/path. ??

On Fri, Sep 2, 2016 at 9:35 AM, mawa316 . <maw...@gmail.com> wrote:
Well, you may be on to something as the element I'm working with is input type "file"....


So, I just have to SendKeys <pathname> and I should be good?  I guess I could add an id to the input element as well to make it easier to ..FindBy.Id.

Hopefully this is the way to go as I would feel more comfortable with test reliability moving in the direction you suggest.


On Fri, Sep 2, 2016 at 9:27 AM, mawa316 . <maw...@gmail.com> wrote:
The way I've gone the route I have is because the browse window that pops up is a Windows based window and not from our web app.

In hunting and pecking around I did see an input type="file" example and it was a browse button that brought up the same Windows File Browse dialog I'm seeing.  So, maybe you're right and SendKeys will work.

I'll inspect the particular element more and see if I can improve upon the test.  Thanks for setting me straight!  You may regret that as I'm sure I'll have more questions!  :-D
On Thu, Sep 1, 2016 at 9:00 PM, Jim Evans <james.h....@gmail.com> wrote:
I'd venture to say that one reason you're getting little response here is that you're entirely bypassing the WebDriver mechanism for uploading files. The method for uploading files is to use the SendKeys method on the WebElement interface. Of course, that mechanism works only on <input type="file"> elements, and many web developers attempt to replace that mechanism because they incorrectly think they have to.

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/zaL_w5enGGI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

mawa316 .

unread,
Sep 2, 2016, 9:39:29 AM9/2/16
to seleniu...@googlegroups.com
Well, you may be on to something as the element I'm working with is input type "file"....


So, I just have to SendKeys <pathname> and I should be good?  I guess I could add an id to the input element as well to make it easier to ..FindBy.Id.

Hopefully this is the way to go as I would feel more comfortable with test reliability moving in the direction you suggest.

On Fri, Sep 2, 2016 at 9:27 AM, mawa316 . <maw...@gmail.com> wrote:
The way I've gone the route I have is because the browse window that pops up is a Windows based window and not from our web app.

In hunting and pecking around I did see an input type="file" example and it was a browse button that brought up the same Windows File Browse dialog I'm seeing.  So, maybe you're right and SendKeys will work.

I'll inspect the particular element more and see if I can improve upon the test.  Thanks for setting me straight!  You may regret that as I'm sure I'll have more questions!  :-D
On Thu, Sep 1, 2016 at 9:00 PM, Jim Evans <james.h....@gmail.com> wrote:
I'd venture to say that one reason you're getting little response here is that you're entirely bypassing the WebDriver mechanism for uploading files. The method for uploading files is to use the SendKeys method on the WebElement interface. Of course, that mechanism works only on <input type="file"> elements, and many web developers attempt to replace that mechanism because they incorrectly think they have to.

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/zaL_w5enGGI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Jim Evans

unread,
Sep 2, 2016, 9:48:54 AM9/2/16
to Selenium Users
No, do not click the browse button. This is a common mistake that WebDriver users often make. All you need to do is use SendKeys sending the full path and file name to the file directly to the <input> element. There is no need to call Click().

mawa316 .

unread,
Sep 2, 2016, 10:06:18 AM9/2/16
to seleniu...@googlegroups.com
WOW!  Here's what appears to work.  Thank you Soooo Much Mr. Evans!!

for (int i = 0; i < testFiles.Length; i++)

{

   driver.FindElementById("fileinput").SendKeys(@"C:\SeleniumTestData\Upload\" + testFiles[i]);

}

Much more concise and I learned how to handle an input element in the process!

mawa316 .

unread,
Sep 2, 2016, 10:52:11 AM9/2/16
to seleniu...@googlegroups.com
Thanks Jim.  I had figured that out when I posted the above code.  You have now idea the sigh of relief I had as a result of your instruction.  What I was doing was flimsy at best.

MUCH Appreciated!!
Reply all
Reply to author
Forward
0 new messages