简体   繁体   English

无法使用Selenium Webdriver中的send_keys上传文件

[英]Unable to Upload File using send_keys in Selenium Webdriver

I'm having trouble getting file uploads to work using Selenium Webdriver with Python. 我在使用Selenium Webdriver和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. 我昨天重新安装了selenium和python,所以我很确定一切都是最新的,如果有帮助的话我会使用Windows 7。 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. 我知道其他人已经问过这个问题,而且每个人都建议的答案是在文件上传元素上使用send_keys命令。 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). 可能相关的一件事是,当我在文件上传表单上尝试send_keys时,selenium抛出一个错误,说该元素不可见,因此无法与之交互(实际上,它是可见的,但显然不是在硒的眼睛里)。 I fixed this problem by running this line of JavaScript beforehand: 我通过事先运行这行JavaScript修复了这个问题:

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

(UploadDocumentPopup) is a parent element of the file input part) (UploadDocumentPopup)是文件输入部分的父元素)

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). 另一个可能有用的消息是,当我使用Selenium 1 / Selenium RC时,我成功地使用了attach_file命令(但是仅支持Firefox)。

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). 点击此链接: https ://qa.infosnap.com/family6/gosnap.aspx?action = 3345&culture = en,点击“继续工作”,然后使用电子邮件aaaa@b.com和密码“asdfjkl;”登录(不包括报价)。 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). 如果没有,只需使用“prev”和“next”来浏览并找到所做的页面(只有3页)。 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. 如果您准备好了,请随意检查页面源,但是FYI'documentfile'是页面源中输入type ='file'元素的名称,最后一行中的xpath指向“upload” “按钮。

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. 更新:我意识到我没有尝试过除了Firefox以外的任何东西,所以我尝试了IE11 - 发生的事情是,当调用send_keys时,本机操作系统文件上传框出现了(我觉得很奇怪,因为我没有单击“浏览”按钮 - 仅使用send_keys),并将文本输入文件名字段。 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. 回顾:在Internet Explorer中,文件上传对话框打开,文件路径进入此对话框,对话框消失,但没有实际附加文件。 In Firefox, no dialogue opens, and still no file attached. 在Firefox中,没有对话框打开,仍然没有附加文件。 Chrome is the same as Firefox. Chrome与Firefox相同。

EDIT: Here's the HTML code for the document upload section: 编辑:这是文档上传部分的HTML代码:

<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. 我还要提一下,我正在寻找一个完全基于selenium的解决方案 - 我知道AutoIt和类似的工具,但我需要远程运行它。

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. 您应该将<filename>替换为您尝试上传的文件的确切名称。 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. 这个版本使用os.getcwd()来获取testscript的当前工作目录,然后将文件名附加到该工作目录的末尾,创建一个可在任何机器上工作的通用路径,而不是指定一个将中断的绝对路径下一台机器。 You shouldn't need the Javascript snippet anymore with this code. 使用此代码不再需要Javascript代码段。

I've used CSS selectors since I prefer them over xpaths, you should be able to convert them easily should you so desire. 我使用CSS选择器,因为我更喜欢它们而不是xpaths,如果你愿意,你应该能够轻松地转换它们。 (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 使用下面的xpath查找带有标记输入和type ='file'的web元素

//input[@type='file'] //输入[@类型= '文件']

which is "documentfile" element in your case. 在您的情况下,这是“documentfile”元素。

Make it visible using javascript: 使用javascript使其可见:

document.getElementById("documentfile").style.visibility = "visible"; 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"); driver.findElement(By.xpath( “//输入[@类型= '文件']”))的SendKeys( “/绝对/ filepath.jpeg”);


Let me know in case of any issues. 如有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM