0
votes

Hi can you help me with uploading a file in Safari ,Mac OS.

I have given a site were we can upload the following types of file by clicking "Choose File" Button(JPG, PNG, GIF, DOC, XLS, PDF, ZIP, RAR, ZIPX)

package Default;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class Safari_demo {

    public static void main(String[] args) throws InterruptedException{

        WebDriver driver = new SafariDriver();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        Thread.sleep(6000);

        driver.get("http://www.files.com/");

        driver.findElement(By.id("uploadFields")).click();

        //can you please help me with code in this line 
        //this is the place were i need to enter the address of the
        //file which is in my system  

        driver.close();
    }

}
3
driver.findElement(By.id("youruploadbuttonid")).sendKeys("yourpicturelocalpath"); - Helping Hands
Were you able to solve this? - Daniel F Jaramillo

3 Answers

2
votes
0
votes

try this.

fd.findElement(By.xpath(".//*[@id='uploadFields']/input"))
                .sendKeys(
                        "your image path");
-1
votes

So the file upload dialog is really a native system dialog not a web one, hence you can not interact with it using Selenium. The way to do it locally is to send the path to filePath textbox, saucelabs describes it pretty well:

For those of you doing this locally, all you need to do is use the sendKeys command to type the local path of the file in any file field. This works like a charm in all drivers. When moving this test to a remote server (such as, for example, our Selenium 2 Cloud), all you need to do is use the setFileDetector method to let WebDriver know that you’re uploading files from your local computer to a remote server instead of just typing a path. Almost magically, the file will be base64 encoded and sent transparently through the JSON Wire Protocol for you before writing the fixed remote path. This is an excellent solution, as it lets you switch your tests from a local to remote Driver without having to worry about changing your tests’ code.

https://saucelabs.com/resources/selenium-file-upload