如何硒的webdriver文件上传到浏览器? [英] how does selenium webdriver upload files to the browser?

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

问题描述

我是一个JavaScript / Java开发人员和我一直在试图从文件系统中找出如何硒的webdriver自动化框架上载文件。这是不可能的设置通过JavaScript文件输入,因为它是一个安全的侵犯。但不知怎么的webdriver可以用下面的命令来做到这一点:

I am a javascript/java developer and I have been trying to figure out how the selenium webdriver automation framework uploads files from the file system. It is impossible to set a file input via javascript because it is a security violation. Yet somehow webdriver is able to do this with the following command:

driver.setFileDetector(new LocalFileDetector());
WebElement upload = driver.findElement(By.id("myfile"));
upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg");
driver.findElement(By.id("submit")).click();

因此​​,他们正在设置发送键,值吗?我不明白这一点。我已经通过源$ C ​​$ C在这里找到看了:
<一href=\"http://$c$c.google.com/p/selenium/source/checkout\">http://$c$c.google.com/p/selenium/source/checkout
我仍然无法找到他们这样做的地方。

So they are setting the value by sending keys to it? I don't get it. I have looked through the source code found here: http://code.google.com/p/selenium/source/checkout I am still not able to find where they do this.

编辑:我的问题不是如何与硒做到这一点,但并硒开发人员如何实现这一目标?他们是如何避开JavaScript中的安全限制?他们如何上传文件?

My question is not how to do this with selenium, but how did the selenium developers make this possible? How did they get around the security restrictions in javascript? How are they uploading the file?

推荐答案

尼斯问题哥们......他们已经写了一个HTTP代理服务器来解决的Javascript secuirty限制。使用该代理使人们有可能侧步不少相同的主持人出身的政策,其中浏览器将不会允许JavaScript做比从当前页面已经被送达服务器之外的其它电话的制约。

Nice question buddy...they have written a HTTP proxy to solve the Javascript secuirty restrictions. Using this proxy made it possible to side-step many of the constraints of the "same host origin" policy, where a browser won't allow Javascript to make calls to anything other than the server from which the current page has been served.

此外webdriver的使用在操作系统级别触发事件的另一种方法。由于这些天然事件不是由浏览器产生的这种方法绕过置于合成的事件,安全限制,因为它们是OS的特定,一旦它们被在特定平台上工作的一个浏览器重新使用在另一code浏览器是相对容易的。

Moreover WebDriver uses the alternative approach of firing events at the OS level. As these "native events" aren't generated by the browser this approach circumvents the security restrictions placed on synthesized events and, because they are OS specific, once they are working for one browser on a particular platform reusing the code in another browser is relatively easy.

大部分内容上面是从引用below..do​​阅读更多细节硒内部下面参考

Most of the content above is referenced from the below..do read the following reference for more details on Selenium internals

http://www.aosabook.org/en/selenium.html

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

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