【问题标题】:Uploading a file in Selenium在 Selenium 中上传文件
【发布时间】:2016-08-29 09:51:57
【问题描述】:

我想上传两个图像 (.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");

谁能帮我做这个???

【问题讨论】:

  • 添加您的代码和 html。
  • 不要将代码粘贴为评论,而是编辑您的问题

标签: java file selenium upload


【解决方案1】:

@sajju - 您可以如下更新您的代码:

它应该对我有用。并根据您的要求优先考虑您的测试方法。仅作为示例,我在这里将优先级设置为 @Test(priority = 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);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2012-01-15
    • 2018-03-31
    • 1970-01-01
    • 2013-06-17
    • 2018-05-30
    • 2017-11-26
    相关资源
    最近更新 更多