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

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

问题描述

我是一名 javascript/java 开发人员,我一直在尝试弄清楚 selenium 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();

所以他们通过发送密钥来设置值?我不明白.我查看了此处找到的源代码:http://code.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.

我的问题不是如何用 selenium 做到这一点,而是 selenium 开发人员是如何做到这一点的?他们是如何绕过 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 安全限制.使用此代理可以绕过同一主机来源"策略的许多限制,其中浏览器不允许 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 使用在操作系统级别触发事件的替代方法.由于这些本机事件"不是由浏览器生成的,因此这种方法绕过了对合成事件的安全限制,并且由于它们是特定于操作系统的,一旦它们在特定平台上为一个浏览器工作,在另一个浏览器中重用代码相对很简单.

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.

上面的大部分内容是从下面引用的..请阅读以下参考以了解有关 Selenium 内部结构的更多详细信息

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

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

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