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

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

问题描述

我已经看到很多关于使用Stackoverflow上的Selenium WebDriver进行文件上传的问题和解决方案。但是没有一个为以下情况工作。





  //假设驱动程序是一个健康的WebDriver实例
WebElement fileInput = driver.findElement(By.name(uploadfile));
fileInput.sendKeys(C:/path/to/file.jpg);

但是我仍然找不到窗口句柄,我该怎么处理呢?





我正在寻找上述情况的解决方案

请检查以下任何一个网站

  http://www.uploadify.com/demos/ 
http://www.zamzar.com/
$ div class =h2_lin>解决方案

  //假设驱动程序是一个健康的WebDriver实例
WebElement fileInput = driver .findElement(By.name(一个 uploadFile));
fileInput.sendKeys(C:/path/to/file.jpg);

嘿,这是我的地方:)。

Zamzar web应该完美地工作。您不要点击元素。你只需要输入路径。具体地说,这应该是绝对可以的:

$ $ $ $ $ $ $ $ $ driver_findElement(By.id(inputFile))。sendKeys C:/path/to/file.jpg);






Uploadify web,因为上传的东西不是 input ,而是一个Flash对象。 WebDriver没有API可以让你使用浏览器对话框(或者Flash对象)。

所以当你点击Flash元素后,会弹出一个窗口以至于你无法控制。在我所知道的浏览器和操作系统中,你几乎可以假定在窗口打开之后,光标位于文件名输入中。请确认这个假设也适用于您的情况。



如果没有,您可以按 Alt + N ,至少在Windows上...



如果是的话,你可以盲目地使用 Robot 类。在你的情况下,这将是以下方式:
$ b $ pre $ driver_findElement(By.id(SWFUpload_0)) 。点击();
机器人r =新机器人();
r.keyPress(KeyEvent.VK_C); // C
r.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中获得)。 b

对于Flash,我知道的唯一选择(来自这个讨论 a>)就是使用黑暗技术:

lockquote
首先,你修改你的flash应用程序的源代码,暴露
使用ActionScript的ExternalInterface API的内部方法。
一旦公开,这些方法将在浏览器中被JavaScript调用。第二,现在JavaScript可以调用Flash应用程序中的内部方法,
您使用WebDriver在网页中进行JavaScript调用,
然后调用您的flash应用程序。

这个技术在文档中进一步解释闪硒项目。
http://code.google.com/p/flash-selenium/ ),但是技术
背后的想法也适用于WebDriver。



I have seen lots of questions and solutions on File upload using Selenium WebDriver on Stackoverflow. But none of the working for following scenario.

Someone has given 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 solution for above scenario

Please check this for any of the following website

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


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


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

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.

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

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

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


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

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.

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.

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天全站免登陆