小编典典

如何在硒中上传多个文件?

java

我正在尝试使用以下代码:-

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

我没有得到结果。


阅读 206

收藏
2020-11-30

共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

希望这给你一个主意

2020-11-30