无法使用Selenium Webdriver中的send_keys上传文件 [英] Unable to Upload File using send_keys in Selenium Webdriver

查看:86
本文介绍了无法使用Selenium Webdriver中的send_keys上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在使用Selenium Webdriver和Python的情况下使文件上传正常工作.昨天我重新安装了selenium和python,因此我可以确定所有内容都是最新的,如果有帮助,请使用Windows 7.我知道其他人已经问过这个问题,每个人都建议的答案是在文件上载元素上使用send_keys命令. 我已经尝试过在其他具有文件上传功能的网页上执行此操作,但我至少可以在另一个网页上使用它,但是当我在要测试的网页上尝试相同的操作时,似乎什么也没发生.我想我记得找到其他无法解决此问题的人的例子,所以我认为我不是唯一遇到此问题的人.

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.

可能相关的一件事是,最初我在文件上传表单上尝试send_keys时,selenium抛出一个错误,指出该元素不可见,因此无法与之交互(实际上是可见的). ,但显然不在硒的眼中).我通过预先运行以下JavaScript行来解决此问题:

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)是文件输入部分的父元素)

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

另一个可能有用的提示是,当我使用Selenium 1/Selenium RC时,我成功使用了attach_file命令(但是仅Firefox支持).

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).

如果有帮助,请按以下步骤进入正在使用的页面.请点击以下链接: https://qa.infosnap.com/family6/gosnap.aspx?action = 3345& culture = en ,单击继续工作",然后使用电子邮件aaaa@b.com和密码"asdfjkl;"登录. (不含引号).然后单击继续工作"链接之一.您进入的页面应具有文档上载"和照片上载"部分.如果不是,请使用上一个"和下一个"浏览,找到需要的页面(只有3页).这是相关的代码-我也尝试了很多其他事情,如果有帮助并且我能记住它们的话,我也很乐意分享.但这是我认为应该"工作的方式.如果愿意,可以随时检查页面源,但是FYI'documentfile'是页面源中输入type ='file'元素的名称,并且最后一行中的xpath指向上载" 按钮.

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()

更新:我意识到除了Firefox之外,我没有尝试过其他方法,所以我尝试了IE11-发生的是,当调用send_keys时,出现了本机OS文件上传框(我认为这很奇怪,因为我并没有点击浏览"按钮-只使用了send_keys),并且文本已输入到文件名字段中.然后文件上传对话框消失了,但是好像什么也没发生.回顾:在Internet Explorer中,文件上传对话框打开,文件路径进入该对话框,对话框消失,但没有实际附加文件.在Firefox中,不会打开任何对话框,并且仍然不会附加任何文件. Chrome与Firefox相同.

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.

这是文档上载部分的HTML代码:

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>

我还应该提到我正在寻找一个完全基于硒的解决方案-我了解AutoIt和类似的工具,但是我需要远程运行它.

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()

在何处应将<filename>替换为您要上载的文件的确切名称.此版本使用os.getcwd()来获取testscript的当前工作目录,然后将文件名附加到该工作目录的末尾,从而创建可在任何计算机上工作的通用路径,而无需指定将在下一台机器上中断的绝对路径.机器.使用此代码,您不再需要Javascript代码段.

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.

我使用CSS选择器是因为我比xpaths更喜欢它们,如果您愿意的话,您应该能够轻松地对其进行转换. (如果真的不能,请发表评论,我会去的)

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)

这篇关于无法使用Selenium Webdriver中的send_keys上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆