简体   繁体   English

Selenium WebDriver- Java-单击“添加文件”按钮以将其上传到应用程序后,文件上传失败

[英]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. 我们正在使用Robot类进行文件上传。 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". 在一个特定区域中,单击“添加文件”按钮后,UI出现错误“上传文件时发生技术错误”。 I have read through various resources and tried using Thread.sleep, Explicit Wait with wait.until(expected conditions) but nothing seems to work. 我已经阅读了各种资源,并尝试使用Thread.sleep,Explicit Wait with wait.until(预期条件),但似乎没有任何效果。 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函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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