Unable to Upload File using send_keys in Selenium Webdriver

431 views
Skip to first unread message

Jack Lindsey

unread,
Jun 24, 2014, 11:23:18 AM6/24/14
to seleniu...@googlegroups.com
This is my first question here so apologies if I’m not doing it quite right.

I’m having trouble getting file uploads to work using Selenium Webdriver with Python.  I reinstalled selenium and python yesterday, so I’m pretty sure everything’s up to date, and I’m using Windows 7 if that helps.  I know others have asked this question, and the answer that everybody recommends is to use the send_keys command on the file upload element.
I’ve tried doing just that on other webpages with file uploads, and I got it to work on at least one other page, but when I try the same procedure on the page I’m trying to test, nothing seems to happen.  I think I remember finding examples of other people who couldn’t get this to work, so I don’t think I’m the only one who’s had this issue. 

 One thing that might be relevant is that originally when I tried send_keys on the file upload form, selenium threw an error saying that the element wasn’t visible and therefore couldn’t be interacted with (it was, in fact, visible, but apparently not in selenium’s eyes).  I fixed this problem by running this line of javascript beforehand:

    document.getElementById('UploadDocumentPopup').style.display = 'block';

(UploadDocumentPopup) is a parent element of the file input part)

Another potentially useful tidbit is that, back when I was using Selenium 1 / Selenium RC, I had success using the attach_file command (which was only supported for Firefox, however).

If it helps, here’s how to get to the page I’m working with.  Follow this link: https://qa.infosnap.com/family6/login.aspx and then login using email aa...@b.com and password “asdfjkl;” (without the quotes).  Then click one of the “continue your work” links.  The page you get to should have document upload and photo upload sections.  If it doesn’t, just use “prev” and “next” to surf around and find the page that does (there’s only like 3 pages).  Here’s the relevant code – I’ve tried a bunch of other things too, which I’m happy to share if it’s helpful and if I can remember them, but this is the way that I think is “supposed” to work. Feel free to check the page source if you're up for it, but FYI 'documentfile' is the name of the input type='file' element in the page source, and the xpath in the last line is pointing to the "upload" button.

    js = "document.getElementById('UploadDocumentPopup').style.display = 'block';"
    wd.execute_script(js)
    wd.find_element_by_link_text("Upload Document...").click()
    wd.find_element_by_id("documentfile").send_keys("C:\\Users\\username\\testdoc.rtf")
    #ActionChains(wd).send_keys(Keys.ESCAPE)
    wd.find_element_by_xpath("//div[@id='modal_container']/div/form/div/input[1]").click()


Thanks!

Mitchell Turlington

unread,
Jun 25, 2014, 8:27:15 AM6/25/14
to seleniu...@googlegroups.com
Your link wouldn't load for me. "System.Web.HttpException: Could not load type 'PageBrandingModule'."
Message has been deleted

Jack Lindsey

unread,
Jun 25, 2014, 12:40:16 PM6/25/14
to seleniu...@googlegroups.com
Try using this link:
https://qa.infosnap.com/family6/gosnap.aspx?action=3345&culture=en
And then clicking "Continue your work"

Kevin O.

unread,
Jun 26, 2014, 5:28:35 PM6/26/14
to seleniu...@googlegroups.com
Changing the visibility through JS should be a last resort, not the first.
There are chunks of the DOM which seem to be duplicated. I have seen this in apps I test.
>>> len(wd.find_elements_by_xpath("(//input[@id='documentfile'])"))
2
Having more than one element with the same id means the HTML is not valid, but that's life in the WWW.
The second one on this page is the one you want to interact with.

This worked for me (picks the last element with the same id):
wd.find_element_by_xpath("(//input[@id='documentfile'])[last()]").send_keys("C:\\Users\\username\\testdoc.rtf")

You could be fancier and filter using is_displayed():
elems = wd.find_elements_by_xpath("//input[@id='documentfile']")
file_button = next(elem for elem in elems if elem.is_displayed())
file_button.send_keys("C:\\Users\\username\\testdoc.rtf")

Jack Lindsey

unread,
Jun 26, 2014, 10:25:58 PM6/26/14
to seleniu...@googlegroups.com
Thanks so much!  I was going crazy trying to figure this out.  Changing the visibility was, in fact, the last thing I tried - never would have guessed there would be hidden duplicate id's floating around...

If you're in the mood to answer one more question which isn't strictly selenium-related, I'm just wondering if you can tell "where" the second documentfile element is?  There only seems to be one in the page source.  I didn't make this page, but the people who did might want to know that their HTML is out of whack.

Thanks again!
Reply all
Reply to author
Forward
0 new messages