在Selenium上传文件

时间:2016-05-04 05:57:46

标签: java file selenium upload

我想上传两个图片(.png)文件。我使用了操作键和发送密钥来上传文件。它对一个文件工作正常但是如果我再次想要上传第二个图像文件,则该文件没有得到上传。

Actions action1 = new Actions(m.driver); 
action1.moveToElement(m.driver.findElement(By.id("onetidIOFile"))).click(); 
WebElement s=m.driver.findElement(By.xpath("//input[@type='file']"));
s.sendKeys("path of one file");m.click("xpath", ".//[@id='attachOKbutton']"); 
m.click("id", "Ribbon.ListForm.Edit.Actions.AttachFile-Large"); 
action1.moveToElement(m.drive‌​r.findElement(By.id("onetidIOFile"))).click(); 
m.driver.findElement(By.xpath("//‌​input[@type='file']")).sendKeys("path of second file "); 
m.key("Enter","attachOKbutton");

有人可以帮我这么做吗?

1 个答案:

答案 0 :(得分:0)

@sajju - 您可以按以下方式更新代码:

它应该起作用,因为它对我有用。并根据您的要求优先考虑您的测试方法。仅举例来说,我在此优先考虑@Test(优先级= 1)。我希望它适合你。

 @Test(priority = 1)
    public void CERTIFICATIONSSCREENUploadCertficationFilesValidation()
            throws InterruptedException, AWTException {

        //Click on File Upload Button
        driver.findElement(By.xpath("//*[@id='certificationFile']")).click();
        Thread.sleep(1000);
        // Set the file name in the clipboard. Also following line of code will search file in your computer so make sure you provide correct file path.

        StringSelection s = new StringSelection("C:\\Doc\\CertificationFile.xls");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s, null);
        Thread.sleep(1000);

        Robot robot1 = new Robot();
        robot1.keyPress(KeyEvent.VK_ENTER);
        robot1.keyRelease(KeyEvent.VK_ENTER);
        robot1.keyPress(KeyEvent.VK_CONTROL);
        robot1.keyPress(KeyEvent.VK_V);
        robot1.keyRelease(KeyEvent.VK_V);
        robot1.keyRelease(KeyEvent.VK_CONTROL);
        robot1.keyPress(KeyEvent.VK_ENTER);
        robot1.keyRelease(KeyEvent.VK_ENTER);
        Thread.sleep(1000);

}