简体   繁体   中英

Unable to Upload File using send_keys in Selenium Webdriver

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/gosnap.aspx?action=3345&culture=en , click "Continue your work" and then login using email aaaa@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()

UPDATE: I realized I hadn't tried this on anything other than Firefox, so I tried IE11 - what happened was, when send_keys was called, the native OS file upload box came up (which I thought was weird, since I didn't click the "browse" button - only used send_keys) and the text was entered into the file name field. Then the file upload dialogue went away, but it was as if nothing ever happened. Recap: in internet explorer, file upload dialogue opens, file path gets entered into this dialogue, dialogue disappears but without actually attaching the file. In Firefox, no dialogue opens, and still no file attached. Chrome is the same as Firefox.

EDIT: Here's the HTML code for the document upload section:

<div id="UploadDocumentPopup" style="display:none;">
    <div class="popupmenu">
        <h1 style="margin-top:0px; padding-bottom:10px; border-bottom:1px solid #CCCCCC;">
            Upload Document
        </h1>
        <p>
            Choose a file to upload.
        </p>
        <form id="documentuploadform" action="services/documentservice.aspx" enctype="multipart/form-data" method="post"
            onsubmit="return AIM.submit(this, {'onStart' : startUploadDocument, 'onComplete' : completeUploadDocument})">
            <input type="file" size="50" id="documentfile" name="documentfile" />
            <input type="hidden" name="cmd" value="upload" />
            <input type="hidden" id="documentuploadfield" name="field" />
            <div style="margin-top: 10px;">
                <input name="ctl00$OutsideFormContentPlaceholder$ctl06" type="submit" value="Upload" />
                <input name="ctl00$OutsideFormContentPlaceholder$ctl07" type="button" onclick="Control.Modal.close();" value="Cancel" />
            </div>
        </form>
    </div>
</div>

I should also mention that I'm looking for an entirely selenium-based solution - I know about AutoIt and similar tools, but I need to run this remotely.

I'd have to see the entire script to make sure the code is correct, but this should give you something to work with:

wd.find_element_by_css_selector('a[onclick*="uploadDocument"]').click()
wd.find_element_by_css_selector('div#UploadDocumentPopup input#documentfile').send_keys(os.getcwd()+"/<filename>")
wd.find_element_by_css_selector('div#UploadDocumentPopup input[value="Upload"]').click()

Where you should replace <filename> with the exact name of the file you are trying to upload. This version uses os.getcwd() to get the current working directory of the testscript and then appends the filename to the end of that working directory, creating a universal path that will work on any machine rather than specifying an absolute path which will break on the next machine. You shouldn't need the Javascript snippet anymore with this code.

I've used CSS selectors since I prefer them over xpaths, you should be able to convert them easily should you so desire. (If you really can't, drop a comment and I'll have a go)

Find web element with tag input and type='file' using below xpath

//input[@type='file']

which is "documentfile" element in your case.

Make it visible using javascript:

document.getElementById("documentfile").style.visibility = "visible";

Send keys on above input element with absolute filepath

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("/absolute/filepath.jpeg");


Let me know in case of any issues.

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