简体   繁体   中英

Selenium WebDriver- Java - File Upload Fails after clicking on Add Files button to upload them to Application

I have an application where I am trying to upload files. We are using Robot class to do the file upload. The files to be uploaded are selected successfully and should be uploaded after clicking on "Add Files" button. There are other areas in the application where we have the similar file upload functionality and files are uploaded successfully. In one particular area, after clicking on Add Files button, I am getting an error on UI " Technical error has occurred while uploading files". I have read through various resources and tried using Thread.sleep, Explicit Wait with wait.until(expected conditions) but nothing seems to work. Have been stuck here for a while. Same code in other areas of the application uploads the file successfully. This code works fine in debug mode but during run time it clicks on "Add Files" button but fails to actually upload file. Snapshot of code snippet:

try{
     System.out.println(filePath);
     File file = new File(filePath);
     System.out.println(file.getAbsolutePath());
     StringSelection ss = new StringSelection(file.getAbsolutePath());
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
     Robot robot = null;
     robot = new Robot();
     robot.keyPress(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_CONTROL);
     robot.delay(3000);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);

   }
    catch (AWTException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
        }
   bStatus=Wait.waitForElementVisibility(By.xpath(Locators.Campaings.FilesTab.completeFileLoadXpath), 90);
    if(!bStatus) return bStatus;

   //add files
   WebDriverWait wait = new WebDriverWait(driver, 10000);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(Locators.Campaings.TaskTab.taskDetailsUploadFilesBtnXpath)));

    bStatus=Elements.clickElement(By.xpath(Locators.Campaings.TaskTab.taskDetailsUploadFilesBtnXpath));
    if(bStatus) 
    {
      Thread.sleep(20000); 
     }

   //WebDriverWait wait = new WebDriverWait(driver,10);
                //wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(Locators.HomePage.MainHeader.MenuLinks.img_Loading_Grid))));
   //bStatus = Wait.waitForElementVisibility(By.xpath(".//*[@id='edit- task-details-modal']//tr//td[1]"), 60);
   //if(!bStatus) return bStatus;
  //TimeUnit.SECONDS.sleep(60);         
     //bStatus=Wait.waitForNoWebElement(By.xpath(Locators.HomePage.MainHeader.MenuLinks.img_Loading_Grid),60);
  //if(!bStatus) return bStatus;

 String actual_Message=Elements.getText(By.id(Locators.Campaings.TaskTab.taskDetailsFileUploadSuccessMessageId)); 

对于上传文件,如果您的DOM类似于“输入类型=“文件”“,则可以使用Selenium的sendkey函数。

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