title: File Upload Using Selenium WebDriver and Java on a Mac
body: <p>I have a web application which I am attempting to automate using Java, TestNG, and Selenium WebDriver. The application navigates through several stages and then the user needs to upload a document. </p>
  
  <p>I have tried the suggestions listed <a href="https://stackoverflow.com/questions/5610256/file-upload-using-selenium-webdriver-and-java">here</a></p>
  
  <p>and have been unable to get it to work. I do not believe that <code>browseBtn.sendKeys(file.getAbsolutePath());</code> is going to work in this case as I am trying to do this on a Mac and there is no cursor for the Browse button.  </p>
  
  <p>Below is my code thus far, just wondering if anyone has been able to successfully do this and how to do it myself (Screenshots of what I am trying to do are below):</p>
  
  <pre><code>    private void documents_UploadDocuments(String driverName) throws SQLException, InterruptedException
  {
      /*
       * Upload documents to a request
       */
              Login.login_Requester(driverName);
              WebDriver driver = Login.driver;
  
              //Click My Requests Tab
              WebElement myRequestTab = driver.findElement(By.xpath("/html/body/div/ul/li[2]/a"));
              myRequestTab.click();
  
              Thread.sleep(1000);
  
              //Click Create a New Request
              WebElement createRequestBtn = driver.findElement(By.id("submit"));
              createRequestBtn.click();
  
              WebElement officialNameTB = driver.findElement(By.id("new-official-name"));
              WebElement createRqtBtn = driver.findElement(By.id("continue"));
              WebElement researchDataAccess = driver.findElement(By.xpath("/html/body/div[3]/div[2]/form/div[2]/div/select/optgroup/option"));
  
              Thread.sleep(1000);
  
              officialNameTB.sendKeys("TEST_UploadDocuments");
              researchDataAccess.click();
              createRqtBtn.click();
  
              Thread.sleep(2500);
  
  
              //Enter an IRB Number
              WebElement irbNumberTB = driver.findElement(By.id("irbNumber"));
              irbNumberTB.click();
              irbNumberTB.sendKeys("This is an IRB #");
  
              //Enter an IRB Expiration Date
              WebElement irbExpCP = driver.findElement(By.id("irbExpDate"));
              irbExpCP.click();
              LocalDate irbDate = LocalDate.now().plusYears(5);
              DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy");
              String irbDate2 = formatter.print(irbDate);
              irbExpCP.sendKeys(irbDate2);
  
              //Enter a Start Date
              WebElement startDateCP = driver.findElement(By.id("startDate"));
              startDateCP.click();
              LocalDate startDate = LocalDate.now();
              String startDate2 = formatter.print(startDate);
              startDateCP.sendKeys(startDate2);
  
              //Enter an End Date
              WebElement endDateCP = driver.findElement(By.id("endDate"));
              endDateCP.click();
              LocalDate endDate = LocalDate.now().plusYears(10);
              String endDate2 = formatter.print(endDate);
              endDateCP.sendKeys(endDate2);
  
              //Click Next Button
              WebElement nextBtn = driver.findElement(By.xpath("/html/body/div/div/div[2]/ul[2]/li[2]/button"));
              nextBtn.click();
  
              Thread.sleep(1000);
  
              //Click Find Participant button
              WebElement findPartBtn = driver.findElement(By.id("findPeopleButton"));
              findPartBtn.click();
              Thread.sleep(1000);
  
              //Search for a participant
              WebElement nameField = driver.findElement(By.id("name"));
              nameField.click();
              nameField.clear();
              String [] namesArray = {"Salisbury, Curtis J. (FVFT)", "Hanson, Shannon H. (FVFT)", "Dinerstein, Jared A. (FVFT)", "Dinerstein, Sabra A. (FVFT)", "Ford, Lee J. (FVFT)"};
              String names = PickRand.get(namesArray);
              nameField.sendKeys(names);
  
              //Find Now Button
              WebElement findBtn = driver.findElement(By.id("findNow"));
              findBtn.click();
  
              //Choose the Participant
              WebElement fullName = driver.findElement(By.id("fullName"));
              fullName.click();
  
              //Choose a location
              String location = PickRand.get(locations.getLocationArray());
              WebElement locationChooser = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div[2]/div[2]/div/table/tbody/tr[2]/td[3]/div/label/div/a/div/b"));
              locationChooser.click();
              WebElement locationSrch = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div[2]/div[2]/div/table/tbody/tr[2]/td[3]/div/label/div/div/div/input"));
              locationSrch.clear();
              locationSrch.sendKeys(location);
              locationSrch.sendKeys(Keys.RETURN);
              Thread.sleep(500);
              WebElement addBtn = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div[2]/div[2]/div/table/tbody/tr[2]/td[6]/button"));
              addBtn.click();
              Thread.sleep(500);
  
              nextBtn.click();
              Thread.sleep(500);
  
              //Click RealSSN Identifier
              WebElement realSSNIdentifier = driver.findElement(By.id("realSSNIdentifier"));
              realSSNIdentifier.click();
  
              //Click CDW Production Domains Data Source
              WebElement cdwProdDomainsCheck = driver.findElement(By.id("1022"));
              cdwProdDomainsCheck.click();
  
              //Click the HIPAA questions
              WebElement hipaaConsentYesRdio = driver.findElement(By.id("HIPAAAuthorization1"));
              WebElement hipaaWaiverYesRdio = driver.findElement(By.id("HIPAAWaiver1"));
              hipaaConsentYesRdio.click();
              hipaaWaiverYesRdio.click();
  
              //Click Next Button
              nextBtn.click();
              Thread.sleep(1000);
  
  
              //Upload Documents
              WebElement uploadDoc1 = driver.findElement(By.id("uploadDocumentLink"));
              uploadDoc1.click();
              Thread.sleep(1000);
              WebElement browseBtn = driver.findElement(By.xpath("id('fileField')"));
              File file = null;
              try
              {
                  file = new File(Documents_UploadDocuments.class.getClassLoader().getResource("/Users/vhaislsalisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf").toURI());
              }
              catch(URISyntaxException e)
              {
                  e.printStackTrace();
              }
              browseBtn.sendKeys(file.getAbsolutePath());
  
  }
  </code></pre>
  
  <p>The upload link opens this screen: 
  <img src="https://i.stack.imgur.com/ZLoan.png" alt="Choose File Modal"></p>
  
  <p>When I click on the Browse button in that modal it opens this screen:
  <img src="https://i.stack.imgur.com/ShqcM.png" alt="File Upload Screen"></p>
  
  <p>So I attempted to use the <code>Robot</code> class as suggested and it is not actually choosing the file. I need it to choose the file, and then go back to the <code>Upload a Document</code> screen so I can click the <code>Upload</code> button to finish the process. When I run the full test, it just stays on the Mac screen for the File Upload. What am I doing wrong?</p>
  
  <p>Below is what I have for using the <code>Robot</code> class:</p>
  
  <pre><code>//Upload Documents
              WebElement uploadDoc1 = driver.findElement(By.id("uploadDocumentLink"));
              uploadDoc1.click();
              Thread.sleep(1000);
              WebElement browseBtn = driver.findElement(By.xpath("id('fileField')"));
              browseBtn.click();
              Robot r = new Robot();
              /*
               * use the Robot class to type out the path of the document
               * "/Users/vhaislsalisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf"
               */
              r.keyPress(KeyEvent.VK_SLASH); 
              r.keyRelease(KeyEvent.VK_SLASH);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_U);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyRelease(KeyEvent.VK_U);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_R);
              r.keyRelease(KeyEvent.VK_R);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_SLASH);
              r.keyRelease(KeyEvent.VK_SLASH);
              r.keyPress(KeyEvent.VK_V);
              r.keyRelease(KeyEvent.VK_V);
              r.keyPress(KeyEvent.VK_H);
              r.keyRelease(KeyEvent.VK_H);
              r.keyPress(KeyEvent.VK_A);
              r.keyRelease(KeyEvent.VK_A);
              r.keyPress(KeyEvent.VK_I);
              r.keyRelease(KeyEvent.VK_I);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_L);
              r.keyRelease(KeyEvent.VK_L);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_A);
              r.keyRelease(KeyEvent.VK_A);
              r.keyPress(KeyEvent.VK_L);
              r.keyRelease(KeyEvent.VK_L);
              r.keyPress(KeyEvent.VK_I);
              r.keyRelease(KeyEvent.VK_I);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_C);
              r.keyRelease(KeyEvent.VK_C);
              r.keyPress(KeyEvent.VK_SLASH);
              r.keyRelease(KeyEvent.VK_SLASH);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_D);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyRelease(KeyEvent.VK_D);
              r.keyPress(KeyEvent.VK_O);
              r.keyRelease(KeyEvent.VK_O);
              r.keyPress(KeyEvent.VK_C);
              r.keyRelease(KeyEvent.VK_C);
              r.keyPress(KeyEvent.VK_U);
              r.keyRelease(KeyEvent.VK_U);
              r.keyPress(KeyEvent.VK_M);
              r.keyRelease(KeyEvent.VK_M);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_N);
              r.keyRelease(KeyEvent.VK_N);
              r.keyPress(KeyEvent.VK_T);
              r.keyRelease(KeyEvent.VK_T);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_SLASH);
              r.keyRelease(KeyEvent.VK_SLASH);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_V);
              r.keyRelease(KeyEvent.VK_V);
              r.keyPress(KeyEvent.VK_I);
              r.keyRelease(KeyEvent.VK_I);
              r.keyPress(KeyEvent.VK_N);
              r.keyRelease(KeyEvent.VK_N);
              r.keyPress(KeyEvent.VK_C);
              r.keyRelease(KeyEvent.VK_C);
              r.keyPress(KeyEvent.VK_I);
              r.keyRelease(KeyEvent.VK_I);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_UNDERSCORE);
              r.keyRelease(KeyEvent.VK_UNDERSCORE);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_T);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyRelease(KeyEvent.VK_T);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_T);
              r.keyRelease(KeyEvent.VK_T);
              r.keyPress(KeyEvent.VK_UNDERSCORE);
              r.keyRelease(KeyEvent.VK_UNDERSCORE);
              r.keyPress(KeyEvent.VK_D);
              r.keyRelease(KeyEvent.VK_D);
              r.keyPress(KeyEvent.VK_O);
              r.keyRelease(KeyEvent.VK_O);
              r.keyPress(KeyEvent.VK_C);
              r.keyRelease(KeyEvent.VK_C);
              r.keyPress(KeyEvent.VK_U);
              r.keyRelease(KeyEvent.VK_U);
              r.keyPress(KeyEvent.VK_M);
              r.keyRelease(KeyEvent.VK_M);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_N);
              r.keyRelease(KeyEvent.VK_N);
              r.keyPress(KeyEvent.VK_T);
              r.keyRelease(KeyEvent.VK_T);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_SLASH);
              r.keyRelease(KeyEvent.VK_SLASH);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_T);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyRelease(KeyEvent.VK_T);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_T);
              r.keyRelease(KeyEvent.VK_T);
              r.keyPress(KeyEvent.VK_UNDERSCORE);
              r.keyRelease(KeyEvent.VK_UNDERSCORE);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_R);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyRelease(KeyEvent.VK_R);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_S);
              r.keyRelease(KeyEvent.VK_S);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_R);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyRelease(KeyEvent.VK_R);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_Q);
              r.keyRelease(KeyEvent.VK_Q);
              r.keyPress(KeyEvent.VK_SHIFT);
              r.keyPress(KeyEvent.VK_M);
              r.keyRelease(KeyEvent.VK_SHIFT);
              r.keyRelease(KeyEvent.VK_M);
              r.keyPress(KeyEvent.VK_E);
              r.keyRelease(KeyEvent.VK_E);
              r.keyPress(KeyEvent.VK_M);
              r.keyRelease(KeyEvent.VK_M);
              r.keyPress(KeyEvent.VK_O);
              r.keyRelease(KeyEvent.VK_O);
              r.keyPress(KeyEvent.VK_PERIOD);
              r.keyRelease(KeyEvent.VK_PERIOD);
              r.keyPress(KeyEvent.VK_P);
              r.keyRelease(KeyEvent.VK_P);
              r.keyPress(KeyEvent.VK_D);
              r.keyRelease(KeyEvent.VK_D);
              r.keyPress(KeyEvent.VK_F);
              r.keyRelease(KeyEvent.VK_F);
              r.keyPress(KeyEvent.VK_ENTER);
              r.keyRelease(KeyEvent.VK_ENTER);
  
              WebElement uploadBtn = driver.findElement(By.id("submit"));
              uploadBtn.click();
  </code></pre>
  
  <p>The <code>Login.driver</code> implementation is below:</p>
  
  <pre><code>public class Login 
  {
  static Browser browser = new Browser();
  HighlightElement highlight = new HighlightElement();
  static WebDriver driver;
  static String user = "UserName";
  static String pwd = "password";
  static int userID;
  
  /*
   * Connection Strings:
   * testDB = "jdbc:jtds:sqlserver:Connection String"
   * devDB = "jdbc:jtds:sqlserver:Connection String"
   */
  
  public static void login_Page(String driverName)
  {
      driver = browser.getDriver(driverName);
  
      //navigate to VINCI login page
      driver.get("http://URL/vinci_dart_client/dart9/index.html");
      driver.manage().window().maximize();
  }
  public static void login_Requester(String driverName) throws InterruptedException, SQLException
  {
      //Change user role to requester
      Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver:Connection String");
      PreparedStatement ps1, ps2, ps3;
      ps1 = con.prepareStatement("select id from hib.person where name = ?");
      ps2 = con.prepareStatement("delete from hib.personrole where personid = ?");
      ps3 = con.prepareStatement("delete from hib.persongroup where personid = ?");
  
      ps1.setString(1, user);
  
      ResultSet rs = ps1.executeQuery();
      while(rs.next())
      {
          userID = rs.getInt(1);
      }
      rs.close();
      ps1.close();
  
      ps2.setInt(1, userID);
      ps3.setInt(1, userID);
  
      ps2.execute();
      ps3.execute();
  
      driver = browser.getDriver(driverName);
  
      //navigate to VINCI login page
      driver.get("http://URL");
      driver.manage().window().maximize();
  
      //Enter User Name
      WebElement userNameField = driver.findElement(By.id("username"));
      userNameField.clear();
      userNameField.sendKeys(user);
  
      //Enter Password
      WebElement pwdField = driver.findElement(By.id("pword"));
      pwdField.clear();
      pwdField.sendKeys(pwd);
  
      //Click Login Button
      WebElement loginBtn = driver.findElement(By.xpath("/html/body/div/div/form/div[4]/div/input"));
      loginBtn.click();
  
  }
  }
  </code></pre>