Java 如何在selenium中上载多个文件?
我正在尝试使用以下代码:-Java 如何在selenium中上载多个文件?,java,eclipse,selenium-webdriver,webdriver,Java,Eclipse,Selenium Webdriver,Webdriver,我正在尝试使用以下代码:- driver.findElement(By.xpath(".//*[@id='attach0']")).sendKeys("first path"+"\n"+"second path""+"\n"third path"); 我没有得到结果。您可以使用AutoIT或JAVA代码。下面我将两者都用作参考。试试他们中的任何一个 import java.io.IOException; import org.junit.AfterClass; impo
driver.findElement(By.xpath(".//*[@id='attach0']")).sendKeys("first path"+"\n"+"second path""+"\n"third path");
我没有得到结果。您可以使用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));
}
}
import java.io.IOException;
导入org.junit.AfterClass;
导入org.junit.BeforeClass;
导入org.junit.Test;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入org.openqa.selenium.support.FindBy;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共类自动上载{
私有静态WebDriver;
私有静态WebDriverWaitForElement;
@FindBy(css=“span.btn.btn success.fileinput按钮”)
私有WebElement添加文件;
@课前
公共静态无效设置(){
DesiredCapabilities desicap=新的DesiredCapabilities();
System.setProperty(“webdriver.chrome.driver”,“D:/WorkSpace/driver/chromedriver.exe”);
desicap=DesiredCapabilities.chrome();
驱动器=新的镀铬驱动器(desicap);
driver.manage().window().maximize();
驱动程序。获取(“https://blueimp.github.io/jQuery-File-Upload/");
waitForElement=新的WebDriverWait(驱动程序,30);
}
@试验
public void AutoitUpload(){
//字符串文件路径=
//“D:/Mine/GitHub/basicprogrammalearn/AutoItScript/unnamed.png”;
WebElement btn=driver.findElement(By.cssSelector(“span.btn.btn success.fileinput按钮”);
字符串文件_dir=System.getProperty(“user.dir”);
字符串cmd=file_dir+“\\AutoItScript\\unnamed.png”;
System.out.println(“文件目录为”+File\u dir);
试一试{
//使用普通
睡眠(3000);
对于(int i=0;i)您不能在一行中上载多个文件,但可以通过循环来尝试。可能存在重复的
#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