Selenium how to upload files to Microsoft Edge - selenium

I am using the following code to upload files to a website to a 'file' type element.
The code works fine in Firefox, Chrome and Safari.
However when I run the code against Edge the file is NOT uploaded
driver.setFileDetector(new LocalFileDetector());
selectFile.sendKeys(path);
This error is reported:
The command failed because the specified element is not pointer or keyboard interactable.
If I try using Javascript like this:
document.getElementById('manual_file_selection').sendKeys(path)
I get this: Object doesn't support property or method 'sendKeys'
As stated the same code works fine in Chrome, Firefox and Safari so I don't understand it.
This is the code behind the file upload button:
<div class="jsx-parser">
<div data-xxxxx-element="manual-file-selection">
<div class="button__container">
<label for="manual_file_selection" class="button button--primary" data-dragging="false" data-xxxxx-element="manual-file-selection--label">
<input id="manual_file_selection" type="file" accept="image/jpeg,image/png" data-xxxxx-element="manual-file-selection--input">
<span>Select File</span>
</label>
</div>
</div>
</div>
Anyone had any success uploading files to Edge with Selenium or is it not supported?

Based on your error messages, I'd give some Javascript a try. It's a bit hacky, as we execute JS to reveal the hidden input element, then send keys to it, but I've had success in the past.
// fetch the element
WebElement input = driver.findElement(By.XPath("//input[#type='file']"));
// run JS to reveal the element
JavascriptExecutor executor = (JavaScriptExecutor)driver;
executor.executeScript("arguments[0].style.display = 'block';", input);
// send file path keys
input.sendKeys(path);
It's worth a try. Let me know if this helps at all.

Related

Button is not clickable in application and no selenium exception found in console

I'm trying to click on button(which I marked in yellow) using xpath, but it is failing to click and more over no selenium exception found. I tried to click with css selector and ID. Using isdisplayed() - output is true. but with isselected() - output is false.
I Kept Webdriver wait (Visibility, element located, element to be clickable), implicit wait and explicit wait. But nothing is working out.
I'm new to selenium, please help me out
Below are the code
<button id="addNewButton-btnEl" class="x-btn-center" type="button" hidefocus="true" role="button" autocomplete="off" style="height: 19px;">

Selenium return webcontent

I am using selenium to automate web gui tests. I would like to know, if selenium can return html which contains rendered html as a result of some button click?
For example, initial html has <div id="errorSection"></div>. Now, selenium clicks on a button and errorSection div tag is filled up like this:
<div id="errorSection">
<ol>
<li>Password must be 8 characters long</li>
<li>Username has already been taken</li>
</ol>
</div>
Is selenium able to send back rendered html with rendered content?

Selenium webdriver java - upload file with phantomjs driver

I am running a selenium webdriver script headless using Phantomjs Driver. I am having issues uploading a file though since on a normal browser (firefox or chrome) it would pop up the OS dialog box that would allow me to locate the file in my machine and upload it.
How to do that with the ghostDriver (Phantomjs Driver)?
Thanks
Always identify & interact with elements of type "file" when uploads are concerned. This would solve your issue of pop ups.
Ex: In my application, upload related elements have the below DOM -
<a id="uploadFileButtonLink" class="uploadFileButtonLink" href="javascript:void(0)" data-uidsfdc="3" style="display: none;">Upload a file</a>
<input id="multiFileInput" class="multifile-upload-input-button" type="file" name="chatterFile_upload" multiple="multiple"/>
<input id="multiUploadBtn" class="btnImportant" type="button" value="Upload Files"/>
In this case, you can use sendKeys method to "multiFileInput" which is of type "file".
This way it would work for all FF, Chrome & also headless browsers.
I am having the same issue and have posted a question for the same. PhantomJS hangs up when using sendKeys() method.
They have an issue logged here - https://github.com/ariya/phantomjs/issues/10993
One of the comments on the issue stated that the below statement worked -
(PhantomJSDriver) driver.executePhantomJS("var page = this; page.uploadFile('input[type=file]', 'path to file');");
You may try the above solution, but it may or may not work.
This code helped me with uploading if 'multiple' attribute was set:
protected void uploadFile(CharSequence... keys) {
if (((WrapsDriver) driver).getWrappedDriver() instanceof PhantomJSDriver) {
StringBuffer s = new StringBuffer(keys.length);
for (int index = 0; index < keys.length; index++) {
s.append(keys[index].toString());
}
((PhantomJSDriver) ((WrapsDriver) driver).getWrappedDriver()).executePhantomJS(
String.format("var page = this; page.uploadFile(arguments[0], '%s');", s.toString()), getElement());
} else {
getElement().sendKeys(keys);
}
}
var webPage = require('webpage');
var page = webPage.create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');
in the new version of phantomjs, you can upload file like this
uploadfile

Watir webdriver: uploading files isn't working in SAFARI browser?

I am trying to attach file to upload with form in safari browser. Element is searchable but file is not attached.
<input type="file" id="file" name="file" maxlength="255" onchange="copyfile();" style="width: 59px; ">
using following command
b.file_field( :id=>"file" ).set "/Users/......../Desktop/abc"
returns
=> "/Users/......../Desktop/abc"
but file not populate in file field
Any ideas?
Issue 4220: The SafariDriver does not support file uploads

How to automate file upload in Internet Explorer using Selenium? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to upload a file from a site using Selenium's java inteface
I am new to Selenium.Can you please tel me how to automate file upload in Internet Explorer using Selenium?
It is not easy, and it is not easy for very good reasons - security. If you are able to upload something like this, what's stopping someone uploading your details using the same method?
You have also given us no example to work with so:
Given this sample webpage:
<html>
<head>
<style type="text/css">
.fileSave { color: red; }
</style>
</head>
<label for="fileUpload">File location:
<input type="file" id="fileUpload" />
<br>
<br>
Save file
</body>
</html>
I can do this, in C#:
Driver = new ChromeDriver();
var fileUploadControl = Driver.FindElement(By.Id("fileUpload"));
fileUploadControl.SendKeys("C:\File.txt");
var submitLink = Driver.FindElement(By.ClassName("fileSave"));
submitLink.Click();
This has been asked several times and is also in some Selenium FAQ.
Selenium 2 (WebDriver) Java example:
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.xpath("//input[#type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");
For Selenium RC, see this question.
The idea is to directly send the absolute path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.