【问题标题】:Selenium WebDriver- Java - File Upload Fails after clicking on Add Files button to upload them to ApplicationSelenium WebDriver- Java - 单击“添加文件”按钮将文件上传到应用程序后文件上传失败
【发布时间】:2018-03-09 14:57:40
【问题描述】:

我有一个尝试上传文件的应用程序。我们正在使用 Robot 类进行文件上传。要上传的文件选择成功,点击“添加文件”按钮后应该上传。应用程序中的其他区域我们具有类似的文件上传功能并且文件已成功上传。在一个特定区域,单击“添加文件”按钮后,我在 UI 上收到错误“上传文件时发生技术错误”。我已经阅读了各种资源并尝试使用 Thread.sleep、Explicit Wait 和 wait.until(expected conditions),但似乎没有任何效果。已经被困在这里一段时间了。应用程序其他区域的相同代码成功上传文件。此代码在调试模式下工作正常,但在运行时它单击“添加文件”按钮但无法实际上传文件。代码 sn -p 截图:

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)); 

【问题讨论】:

    标签: java selenium-webdriver file-upload wait robotframework


    【解决方案1】:

    对于上传文件,如果你的 DOM 像 '输入类型="文件"' 然后就可以使用 Selenium 的 sendkey 功能了。

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 2017-06-16
      相关资源
      最近更新 更多