如何在硒的文件上传,没有文本框 [英] How to upload a file in Selenium with no text box

查看:147
本文介绍了如何在硒的文件上传,没有文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个解决方案,以硒2上传文件。

I have been looking for a solution to uploading a file in Selenium 2.

问题是,我想要上传的网页元素有两种方式可用:
拖放,或点击一个按钮。有没有现场输入框。不,我还没有使用的SendKeys尝试。我试了一下按钮,所有的周边元素。

The problem is that the web element that I'm trying to upload is usable in two ways: Drag and drop, or click on a button. There is no field entry box. Not that I haven't tried using sendKeys. I've tried it on the button, and all surrounding elements.

第二部分对这一问题的是,我写了一个Windows机器上,但Linux机器上发生的自动化。这意味着AutoIt的将无法工作。这是上传框的HTML。

The second part to this problem is that I write on a Windows machine, but the automation occurs on a Linux machine. That means that AutoIt won't work. This is the HTML of the upload box.

<div class="up-target" id="up-drop-zone">
    <div class="up-drop-zone-pre hidden">
        <p>Please choose a folder to upload</p>
    </div>
    <div class="up-drop-zone-decor">
        <p>Drop one or more files here</p>
        <p>or</p>
        <button name="uploadFile" class="upload">Select Files</button>
        <input type="file" id="up-drop-zone-input" name="files[]" multiple="true">
    </div>
</div>

我使用Java和开放以外硒(但是,我只有选择Maven仓库)。

I am using Java, and open to other methods outside of Selenium (However, I do only have select maven repositories).

感谢您!

推荐答案

不幸的是,你不能这样做,截至目前(2013年1月,硒2.29.1),因为硒不支持 &LT;输入类型=文件多种方式&gt; 元素

Unfortunately, you can't do that as of now (January 2013, Selenium 2.29.1), because Selenium doesn't support <input type="file" multiple> elements.

有一个功能增强请求这个由项目开发商自己做,它只是尚未实现。你可以加注星标有在优先级列表中向上移动。

There is a feature enhancement request for this made by the project developers themselves, it's just not yet implemented. You can star it there to move it upwards in the priority list.

另外,据我所知,你不能真正从桌面可靠地拖动文件到 WebElement

Also, as far as I know, you can't really drag a file from desktop to a WebElement in a reliable way.

一个解决办法可能是使用的AutoIt (仅Windows)或 机器人 类(也将对你类似的设置才有效),然后键入路径盲目地进入对话框:

A workaround might be the use of AutoIT (Windows only) or the Robot class (will also work only on setups similar to yours) and type the path "blindly" into the dialog:

driver.findElement(By.id("up-drop-zone-input")).click();
Robot r = new Robot();
r.keyPress(KeyEvent.VK_C);        // C
r.keyRelease(KeyEvent.VK_C);
r.keyPress(KeyEvent.VK_COLON);    // : (colon)
r.keyRelease(KeyEvent.VK_COLON);
r.keyPress(KeyEvent.VK_SLASH);    // / (slash)
r.keyRelease(KeyEvent.VK_SLASH);
// etc. for the whole file path

r.keyPress(KeyEvent.VK_ENTER);    // confirm by pressing Enter in the end
r.keyRelease(KeyEvent.VK_ENTER);

这很烂,但它应该工作。请注意,您可能需要这些:如何让机器人类型`:`? 字符串转换为的KeyEvent (再加上有新的,有光泽的 的KeyEvent#getExtendedKey codeForChar() 它做类似的工作,但只能从JDK7)。

It sucks, but it should work. Note that you might need these: How can I make Robot type a `:`? and Convert String to KeyEvents (plus there is the new and shiny KeyEvent#getExtendedKeyCodeForChar() which does similar work, but is available only from JDK7).

这篇关于如何在硒的文件上传,没有文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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