简体   繁体   中英

Uploading file using selenium WebDriver (java) if their is no text-area to send file path

I was trying to upload a file in "dropbox.com" using selenium WebDriver, when i click on choose file it pops up a window automatically, their is no text area to send file path. how to handle this kind of situation? and in some situation people use Robot class but i did not understand what is the use of it, i read in java documentation but i did not get clear idea, could some one help on this please, help is greatly appreciated.

1. Using sendKeys():

If the input type = file then you can easily use the sendKeys() method and pass the file's absolute path to the Choose file control.

//Find the element of upload button and send the path
WebElement element= driver.findElement(By.name("datafile"));
element.sendKeys("C:\Users\Easy\Desktop\testfile.txt");

2. Using AutoIT:

Since the file upload window is a Windows control, you will have to use a Windows automation tool like AutoIT to send your file path. With AutoIT you can create an .au3 file and compile it to a .exe and invoke this exe file from your Selenium script.

Runtime.getRuntime().exec("your exe file");

And the AutoIT code would be:

WinWaitActive("File Upload") 
Send("Full path of the document")
Send("{ENTER}")

3. Using Robot class:

Using Robot class, you can copy the file's absolute path to your clipboard and then paste it into the Windows File upload window.

setClipboardData(fileLocation);
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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