How to upload a file by transfering the file from the local machine to the remote web server using Selenium Grid
This error message...
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Attempting to upload file 'C:\daten\test2.xml' which does not exist.
...implies that the desired file doesn't exist on the client machine.
Local file detector
The Local File Detector allows the transfer of files from the client machine to the remote server. In case a test needs to upload a file to a web application, a remote WebDriver can automatically transfer the file from the local machine to the remote web server during runtime. This allows the file to be uploaded from the remote machine running the test. It is not enabled by default and can be enabled as follows:
Java:
driver.setFileDetector(new LocalFileDetector());
Python:
from selenium.webdriver.remote.file_detector import LocalFileDetector
driver.file_detector = LocalFileDetector()C#:
var allowsDetection = this.driver as IAllowsFileDetection;
if (allowsDetection != null)
{
allowsDetection.FileDetector = new LocalFileDetector();
}Ruby:
@driver.file_detector = lambda do |args|
# args => ["/path/to/file"]
str = args.first.to_s
str if File.exist?(str)
endJavaScript:
var remote = require('selenium-webdriver/remote');
driver.setFileDetector(new remote.FileDetector);Kotlin:
driver.fileDetector = LocalFileDetector()
This usecase
If you are running your tests on Selenium Grid then you need to let your remote driver know that the file that needs to be uploaded is residing on the local machine and not on remote machine. In those cases, to upload a file from the client machine to the remote server, WebDriver can automatically transfer the file from the local machine to the remote web server during runtime you can use the following code block:
WebElement addFile = driver.findElement(By.xpath("//input[@type='file']"));
((RemoteWebElement)addFile).setFileDetector(new LocalFileDetector());
addFile.sendKeys("C:\\daten\\test2.xml");
Outro
Selecting and uploading files while running your tests on Selenium Grid
How to upload file using Selenium Webdriver?
Below code solved the issue..
cd.findElement(By.id("import_file")).sendKeys("E:\\iMedicor-Karthik\\2.Demographics\\Patients_Data\\Patient_One.xml");
Actually the filepath caused a issue for me.. I have used
E:\\iMedicor-Karthik\\2.Demographics\\Patients_Data\\Patient_One.xml
instead of E://iMedicor-Karthik/2.Demographics/Patients_Data/Patient_One.xml
How to handle windows file upload using Selenium WebDriver?
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");
Hey, that's mine from somewhere :).
In case of the Zamzar web, it should work perfectly. You don't click the element. You just type the path into it. To be concrete, this should be absolutely ok:
driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg");
In the case of the Uploadify web, you're in a pickle, since the upload thing is no input
, but a Flash object. There's no API for WebDriver that would allow you to work with browser dialogs (or Flash objects).
So after you click the Flash element, there'll be a window popping up that you'll have no control over. In the browsers and operating systems I know, you can pretty much assume that after the window has been opened, the cursor is in the File name
input. Please, make sure this assumption is true in your case, too.
If not, you could try to jump to it by pressing Alt + N, at least on Windows...
If yes, you can "blindly" type the path into it using the Robot
class. In your case, that would be something in the way of:
driver.findElement(By.id("SWFUpload_0")).click();
Robot r = new Robot();
r.keyPress(KeyEvent.VK_C); // C
r.keyRelease(KeyEvent.VK_C);
r.keyPress(KeyEvent.VK_COLON); // : (colon)
r.keyRelease(KeyEvent.VK_COLON);
r.keyPress(KeyEvent.VK_SLASH); // / (slash)
r.keyRelease(KeyEvent.VK_SLASH);
// etc. for the whole file path
r.keyPress(KeyEvent.VK_ENTER); // confirm by pressing Enter in the end
r.keyRelease(KeyEvent.VK_ENTER);
It sucks, but it should work. Note that you might need these: How can I make Robot type a `:`? and Convert String to KeyEvents (plus there is the new and shiny KeyEvent#getExtendedKeyCodeForChar()
which does similar work, but is available only from JDK7).
For Flash, the only alternative I know (from this discussion) is to use the dark technique:
First, you modify the source code of you the flash application, exposing
internal methods using the ActionScript's ExternalInterface API.
Once exposed, these methods will be callable by JavaScript in the browser.Second, now that JavaScript can call internal methods in your flash app,
you use WebDriver to make a JavaScript call in the web page, which will
then call into your flash app.This technique is explained further in the docs of the flash-selenium project.
(http://code.google.com/p/flash-selenium/), but the idea behind the technique
applies just as well to WebDriver.
How to upload file using python selenium-webdriver when there is no input type but instead it has a button type in HTML?
There is a hidden input with type=file
. To upload file using Selenium yo have to send keys to input[type=file]
:
browser.find_element_by_css_selector(".file-upload-input input[type=file]").send_keys('C:\\Users\\Desktop\\test.png')
Use WebDriverWait
to wait element to be present in the DOM:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ...
wait = WebDriverWait(driver, 5)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".file-upload-input input[type=file]"))).send_keys('C:\\Users\\Desktop\\test.png')
Related Topics
How to Get Web Page's Content and Save It into the String Variable
C# Get a Control's Position on a Form
Add Separator to String at Every N Characters
ASP.NET Core Model Binding Error Messages Localization
Winforms: Application.Exit VS Environment.Exit VS Form.Close
Compare Two Lists for Differences
Hosting Clr in Delphi With/Without Jcl - Example
One Class Per File Rule in .Net
How to "Await Yield Return Dosomethingasync()"
Comparing Timer with Dispatchertimer
The Type Arguments for Method Cannot Be Inferred from the Usage
Convert Datetime to Date Format Dd/Mm/Yyyy
Split String into String Array of Single Characters
Iis Server & ASP.NET Core - 500.19 with Error Code 0X8007000D on Httpplatformhandler Tag
Nullable Reference Types: How to Specify "T" Type Without Constraining to Class or Struct
What Is the Best Practice for "Copy Local" and with Project References