如何使用 Selenium WebDriver 处理 Windows 文件上传? [英] How to handle windows file upload using Selenium WebDriver?

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

问题描述

我在 Stack Overflow 上看到了很多关于使用 Selenium WebDriver 上传文件的问题和解决方案.但是它们都不适用于以下场景.

I have seen lots of questions and solutions on File upload using Selenium WebDriver on Stack Overflow. But none of them are working for following scenario.

有人给出了如下解决方案

Someone has given a solution as following

// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");

但是我还是找不到窗口句柄.我该怎么做?

But still I can't find window handle. How can I work on that?

我正在为上述场景寻找解决方案.

I am looking for a solution for the scenario above.

请在以下任一网站上查看.

Please check this on any of the following websites.

http://www.uploadify.com/demos/
http://www.zamzar.com/

推荐答案

// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");

嘿,那是我从某个地方弄来的 :)

Hey, that's mine from somewhere :).

对于 Zamzar 网络,它应该可以完美运行.您不要点击该元素.您只需在其中输入路径即可.具体来说,这应该绝对没问题:

In case of the Zamzar web, it should work perfectly. You don't click the element. You just type the path into it. To be concrete, this should be absolutely ok:

driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg");


Uploadify 网络的情况下,您陷入困境,因为上传的不是input,而是Flash 对象.WebDriver 没有允许您使用浏览器对话框(或 Flash 对象)的 API.


In the case of the Uploadify web, you're in a pickle, since the upload thing is no input, but a Flash object. There's no API for WebDriver that would allow you to work with browser dialogs (or Flash objects).

因此,在您单击 Flash 元素后,会弹出一个您无法控制的窗口.在我知道的浏览器和操作系统中,您几乎可以假设在窗口打开后,光标位于 File name 输入中.请确保这个假设也适用于您的情况.

So after you click the Flash element, there'll be a window popping up that you'll have no control over. In the browsers and operating systems I know, you can pretty much assume that after the window has been opened, the cursor is in the File name input. Please, make sure this assumption is true in your case, too.

如果没有,您可以尝试通过按 Alt + N 跳转到它,至少在 Windows 上...

If not, you could try to jump to it by pressing Alt + N, at least on Windows...

如果是,您可以盲目地"使用 Robot 类.在您的情况下,这会妨碍:

If yes, you can "blindly" type the path into it using the Robot class. In your case, that would be something in the way of:

driver.findElement(By.id("SWFUpload_0")).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);

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

对于 Flash,我知道的唯一替代方案(来自 本次讨论) 是使用暗技术:

For Flash, the only alternative I know (from this discussion) is to use the dark technique:

首先,你修改你的flash应用的源代码,暴露使用 ActionScript 的 ExternalInterface API 的内部方法.一旦公开,这些方法将可以在浏览器中被 JavaScript 调用.

First, you modify the source code of you the flash application, exposing internal methods using the ActionScript's ExternalInterface API. Once exposed, these methods will be callable by JavaScript in the browser.

其次,既然 JavaScript 可以在您的 Flash 应用程序中调用内部方法,您使用 WebDriver 在网页中进行 JavaScript 调用,这将然后调用您的 Flash 应用.

Second, now that JavaScript can call internal methods in your flash app, you use WebDriver to make a JavaScript call in the web page, which will then call into your flash app.

在 flash-selenium 项目的文档中进一步解释了这种技术.(http://code.google.com/p/flash-selenium/),但技术背后的想法也同样适用于 WebDriver.

This technique is explained further in the docs of the flash-selenium project. (http://code.google.com/p/flash-selenium/), but the idea behind the technique applies just as well to WebDriver.

这篇关于如何使用 Selenium WebDriver 处理 Windows 文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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