10

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!

4

4 回答 4

9

不幸的是,你现在不能这样做(2013 年 1 月,Selenium 2.29.1),因为 Selenium 不支持<input type="file" multiple>元素。

项目开发人员自己对此提出了功能增强请求,但尚未实施。您可以在此处加注星标以将其在优先级列表中向上移动。

此外,据我所知,您不能真正将文件从桌面拖到一个WebElement可靠的方式。

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

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

这很糟糕,但它应该工作。请注意,您可能需要这些:如何让 Robot 键入 `:`?Convert String to KeyEvents(还有新的和闪亮的KeyEvent#getExtendedKeyCodeForChar(),它做类似的工作,但只能从 JDK7 获得)。

于 2013-01-31T14:31:39.023 回答
1

我发现我可以让它工作的唯一方法是使用AutoIt(感谢LittlePandauser3903359的回答)。

我改进了脚本,因为我发现在测试运行时执行任何其他操作可能会阻止它工作。诀窍是找到窗口,然后在输入文本之前使其处于活动状态。

超时是为了防止多个 AutoIt 脚本在后台挂起,这意味着当您停止测试并尝试做自己的工作时,它们会开始并尝试开始输入!

请注意,窗口在不同浏览器中的名称不同(例如,Chrome 中的“打开”)。

$windowHandle = WinWait("Choose File to Upload", "", 3) ; 3 second timeout - NB the window name will be different in different browsers!

If $windowHandle == 0 Then
   MsgBox(0, "", "Upload popup not found")
Else
   ;MsgBox(0, "", "Upload popup found: " & $windowHandle)
   WinActivate($windowHandle)
   Send("C:\\path\to\myfile.txt")
   Send("{ENTER}")
EndIf

根据所有其他答案,我假设从 Java 运行 AutoIt 脚本:

Runtime.getRuntime().exec("MyAutoItScript.exe");

从 C# 运行 AutoIt 脚本:

var process = Process.Start(@"C:\\path\to\myAutoItScript.exe");
process.WaitForExit();
Thread.Sleep(200); // IE fix for Modal dialog present exception
于 2018-02-02T08:28:35.087 回答
0

I thought autoIT will solve the problem just a part of the java code in my test

String[] commands = new String[]{};
commands = new String[]{"c:/test/attachFile.exe"};
Runtime.getRuntime().exec(commands);
于 2013-07-15T07:25:15.360 回答
0

像这样试试

driver.findElement(By.id("up-drop-zone-input")).sendKeys("filePath");
于 2013-01-30T04:42:50.117 回答