如何使用selenium webdriver附加文件?

时间:2014-05-07 06:23:30

标签: java selenium

我想使用Selenium webdriver添加附件。我的输入标签看起来像这样。

Input代码

<input id="5465465456461763_input" type="file" name="_attFile_" multiple="" tabindex="0"/>

我正在使用此代码

driver.findElement(By.xpath("//input[@name='_attFile_']")).sendKeys("D:/foldername/filename");

输出:0B已上传

这种方法可能有什么问题?

2 个答案:

答案 0 :(得分:0)

我请求您使用AutoIt脚本:

用于上传fileWinWaitActive的窗口标题(“文件  上传“);

文件为uploadedSend("C:\samplefile.txt") Send("{ENTER}")

将此文件另存为AddFile.au3。

然后使用以下代码调用exe文件

通过编译上述AutoIt脚本

创建
try {                    
String[] commands = new String[]{};             
commands = new String[]{"C:\\Program Files (x86)\\AutoIt3\\Examples\\Addfile.exe"}; //location of the autoit executable   
Runtime.getRuntime().exec(commands);    
} 
catch (IOException e) 
{
}

WebElement addFileElement = driver.findElement(By.className("add-file")); // you can find the element to upload the file in different way,  class name used  here, you can use xpath

 addFileElement.click();

答案 1 :(得分:0)

IF WinExists("File Upload") Then
   WinWaitActive("File Upload")
   ControlSetText("File Upload","",1148,"C:\src.zip")
   ControlClick("File Upload","",1)


Else
   MsgBox(1,"ERROR","UNABLE TO VIEW THE FOLDER")
EndIf

这是自动的.au3文件。 你需要安装auto并创建任何文件,比如&#34; upload.au3&#34; 右键单击此文件并编译它以创建upload.exe 现在,在webdriver代码中,编写以下代码

---->>>
        driver.findElement(By.xpath("//input[@name='_attFile_']")).click();
        Thread.sleep(5000);
        Runtime.getRuntime().exec("F:\\AutoIt\\upload.exe");
---->>>