如何在selenium上传多个文件?

时间:2016-06-21 10:16:54

标签: java eclipse selenium-webdriver webdriver

我试图使用以下代码: -

driver.findElement(By.xpath(".//*[@id='attach0']")).sendKeys("first path"+"\n"+"second path""+"\n"third path");

我没有得到结果

请帮助我!

1 个答案:

答案 0 :(得分:1)

您可以使用AutoIT或JAVA代码。下面我用了两个供你参考。试试他们中的任何人

    import java.io.IOException;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class AutoITforUpload {

        private static WebDriver driver;
        private static WebDriverWait waitForElement;

        @FindBy(css = "span.btn.btn-success.fileinput-button")
        private WebElement Add_files_btn;

        @BeforeClass
        public static void setUp() {
            DesiredCapabilities desicap = new DesiredCapabilities();
            System.setProperty("webdriver.chrome.driver", "D:/WorkSpace/Driver/chromedriver.exe");
            desicap = DesiredCapabilities.chrome();
            driver = new ChromeDriver(desicap);
            driver.manage().window().maximize();
            driver.get("https://blueimp.github.io/jQuery-File-Upload/");
            waitForElement = new WebDriverWait(driver, 30);
        }

        @Test
        public void AutoitUpload() {
            // String filepath =
            // "D:/Mine/GitHub/BasicProgramLearn/AutoItScript/unnamed.png";
            WebElement btn = driver.findElement(By.cssSelector("span.btn.btn-success.fileinput-button"));
            String file_dir = System.getProperty("user.dir");
            String cmd = file_dir + "\\AutoItScript\\unnamed.png";

            System.out.println("File directory is " + file_dir);

            try {
                // Using ordinary
                Thread.sleep(3000);

    for(int i=0;i<3;i++) //multiple times upload ;
                driver.findElement(By.xpath("//*[@id='fileupload']/div/div[1]/span[1]/input")).sendKeys(cmd);
//use any String Array for multiple files 

                waitForElement(btn);

                btn.click();
                Thread.sleep(3000);
                System.out.println(file_dir + "/AutoItScript/FileUploadCode.exe");
                Runtime.getRuntime().exec(file_dir + "\\AutoItScript\\ChromeFileUpload.exe" + " " + cmd);
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
        }

        @AfterClass
        public static void TearDown() {
            try {
                Thread.sleep(5000);
                driver.quit();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        private void waitForElement(WebElement vElement) {
            waitForElement.until(ExpectedConditions.visibilityOf(vElement));
        }

    }

AutoIt中的代码是

#include<IE.au3>
If $CmdLine[0] < 2 Then
$window_name="Open"
WinWait($window_name)
ControlFocus($window_name,"","Edit1")
ControlSetText($window_name,"","Edit1",$CmdLine[1])
ControlClick($window_name,"","Button1")
EndIf

希望这会给你一个想法