Reputation: 463
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.
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). 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. Makes sense as well...
I have found out though, that a Firefox Add-On is supposed to be able to do this. The closest I have come to an answer is another Stack Overflow thread on this topic. 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)
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. 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? Is this supposed to work?
2. How do I make this work? I am not sure whether the mentioning of the "chrome authority" in the above link is relevant for me. Do I have to extend the scripts privileges somehow? If so, how?
Thank you!
Sandro
P.S. When I tried to set the value via jQuery, Firefox crashed completely.
P.P.S. At the moment this is only supposed to work on my personal machine, cross browser issues or similar things thus aren't relevant
Upvotes: 4
Views: 5113
Reputation: 463
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. I then tried it with the C# implementation with a quick Powershell script and it worked like a charme.
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:
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
Upvotes: 2