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

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

问题描述

我一直在寻找在 Selenium 2 中上传文件的解决方案.

问题是我尝试上传的网页元素有两种用途:拖放或单击按钮.没有字段输入框.并不是说我没有尝试过使用 sendKeys.我已经在按钮和所有周围元素上试过了.

这个问题的第二部分是我在 Windows 机器上编写,但自动化发生在 Linux 机器上.这意味着 AutoIt 将不起作用.这是上传框的 HTML.

<div class="up-drop-zone-pre hidden"><p>请选择要上传的文件夹</p>

<div class="up-drop-zone-decor"><p>在此处放置一个或多个文件</p><p>或</p><button name="uploadFile" class="upload">选择文件</button><input type="file" id="up-drop-zone-input" name="files[]" multiple="true">

我正在使用 Java,并且对 Selenium 之外的其他方法开放(但是,我只有选择的 maven 存储库).

谢谢!

解决方案

不幸的是,截至目前(2013 年 1 月,Selenium 2.29.1),您不能这样做,因为 Selenium 不支持 元素.

有一个功能增强请求项目开发人员自己,它只是尚未实施.您可以在此处加星标以将其在优先级列表中向上移动.

此外,据我所知,您无法真正以可靠的方式将文件从桌面拖到 WebElement.

解决方法可能是使用 AutoIT(仅限 Windows)或 Robot 类(也仅适用于与您类似的设置)并在对话框中盲目地"输入路径:

driver.findElement(By.id("up-drop-zone-input")).click();机器人 r = 新机器人();r.keyPress(KeyEvent.VK_C);//Cr.keyRelease(KeyEvent.VK_C);r.keyPress(KeyEvent.VK_COLON);//: (冒号)r.keyRelease(KeyEvent.VK_COLON);r.keyPress(KeyEvent.VK_SLASH);///(斜线)r.keyRelease(KeyEvent.VK_SLASH);//整个文件路径等r.keyPress(KeyEvent.VK_ENTER);//最后按回车确认r.keyRelease(KeyEvent.VK_ENTER);

它很糟糕,但它应该可以工作.请注意,您可能需要这些:如何使机器人类型成为`:`?将字符串转换为 KeyEvents(此外还有新的闪亮的 KeyEvent#getExtendedKeyCodeForChar() 做类似的工作,但只能从 JDK7 中获得).

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

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.

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>

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

Thank You!

解决方案

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.

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

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

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

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

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆