简体   繁体   English

组 <input type=“file”> javascript在Firefox附加组件中的附加值

[英]Set <input type=“file”> value in Firefox Add-on with javascript

I have the following scenario in front of me: 我眼前有以下情形:

I am using Selenium to drive my browser to fill a few forms, which works really well. 我正在使用Selenium来驱动我的浏览器填写一些表格,这确实很好。

Now I am at the point, where I also want to upload files via the given webform (can't change how it's processed, can only manipulate the Javascript/HTML/DOM). 现在,我正要通过给定的Webform上传文件(无法更改其处理方式,只能操作Javascript / HTML / DOM)。 As I have found out, for security reasons the file upload window pops up right away, when clicking into the field. 我发现,出于安全原因,单击该字段时会立即弹出文件上传窗口。 And also manipulation via normal Javascript isn't possible. 而且不可能通过普通的Javascript操作。 Makes sense as well... 也是有道理的...

I have found out though, that a Firefox Add-On is supposed to be able to do this. 不过,我发现Firefox插件应该可以做到这一点。 The closest I have come to an answer is another Stack Overflow thread on this topic . 我得到的最接近答案是关于该主题的另一个Stack Overflow线程 If I got this working for me, that would be awesome! 如果我为我工作,那真是太棒了! :-) But unfortunately I didn't, yet... :-)但不幸的是我还没有...

Minimal example: 最小示例:

<form method="post" url="http://www.example.com">
<input type="file" id="fileInput"/>
</form>

when calling (from the console in Firebug) 调用时(从Firebug中的控制台)

document.getElementById('fileInput').value = "C:\\image.png";

in get "Error: The operation is insecure." 在得到“错误:操作不安全。” which is what I expect. 这是我所期望的。

When calling the same in a contentScriptFile in a Firefox Add-On (I am using the add-on builder to create it) the script simply breaks without an error. 在Firefox附加组件的contentScriptFile中调用相同的脚本时(我正在使用附加组件生成器来创建脚本),脚本会简单地中断而不会出现错误。 My indicator for that is, that I do see the alerts before, but none after that line 我的指示是,我确实在此行之前看到了警报,但在该行之后没有看到警报

Now the question is not so much why I don't get an error, but: 现在的问题不是为什么我没有收到错误,而是:
1. Am I on the right track? 1.我走在正确的轨道上吗? Is this supposed to work? 这应该工作吗?
2. How do I make this work? 2.如何进行这项工作? I am not sure whether the mentioning of the "chrome authority" in the above link is relevant for me. 我不确定以上链接中提及“ chrome权威”是否对我有用。 Do I have to extend the scripts privileges somehow? 我是否必须以某种方式扩展脚本特权? If so, how? 如果是这样,怎么办?

Thank you! 谢谢!
Sandro 桑德罗

PS When I tried to set the value via jQuery, Firefox crashed completely. PS当我尝试通过jQuery设置值时,Firefox完全崩溃了。
PPS At the moment this is only supposed to work on my personal machine, cross browser issues or similar things thus aren't relevant PPS目前,此功能仅应在我的个人计算机上运行,​​跨浏览器问题或类似问题,因此不相关

I have now found a solution, that works. 我现在找到了一个可行的解决方案。 Part of the problems seems to have been that I have been using the Selenium VBA wrapper. 问题的一部分似乎是我一直在使用Selenium VBA包装器。 I then tried it with the C# implementation with a quick Powershell script and it worked like a charme. 然后,我使用带有快速Powershell脚本的C#实现对它进行了尝试,它的工作方式像个迷住了。

Summary: Original goal: upload a file wit a given form using Selenium Solution: use SendKeys method on the input element of type 'file' to give full path to file Sample script in Powershell: 摘要:最初的目标:使用Selenium解决方案上载给定格式的文件解决方案:在类型'file'的输入元素上使用SendKeys方法提供Powershell中文件示例脚本的完整路径:

add-type -path C:\path\To\the\dll\on\your\harddrive\webdriver.dll
$driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver
#the following page contains an element <input type='file' id='fileInput'/>
$driver.Url = "http://pathToTheSite.com/fileUploadFormPage.html"

$element = $driver.FindElementById("fileInput")

$element.SendKeys("C:\Image.png")

Done!!! 做完了!!! The file is attached and the form submitted in one go 随附文件,一次性提交表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM