如何使用Selenium Webdriver上传文件?

时间:2015-09-25 11:21:15

标签: java selenium file-upload selenium-webdriver upload

尝试使用selenium webdriver在stackoverflow上提供有关上传文件的大量建议,但问题仍然存在。

用户点击“浏览”按钮,然后会显示一个新的弹出窗口,然后脚本将尝试上传该文件。我的代码如下:

cd.findElement(By.xpath("//*[@id='import_file']")).sendKeys("E://iMedicor - Karthik/2.Demographics/Patients_Data/Patient_One.xml");

enter image description here

如果单击“浏览”按钮,则会显示以下弹出窗口。 enter image description here

文件在E盘内。尝试下面的脚本,但问题仍然相同。

cd.findElement(By.xpath("//*[@id='import_file']")).click();
cd.switchTo().activeElement()
  .sendKeys("E://iMedicor - Karthik/2.Demographics/Patients_Data/Patient_One.xml");

1 个答案:

答案 0 :(得分:0)

下面的代码解决了这个问题..

cd.findElement(By.id("import_file")).sendKeys("E:\\iMedicor-Karthik\\2.Demographi‌​cs\\Patients_Data\\Patient_One.xml");

实际上文件路径对我造成了问题..我已经使用了

E:\\iMedicor-Karthik\\2.Demographics\\Patients_Data\\Patient_One.xml

而不是E://iMedicor-Karthik/2.Demographics/Patients_Data/Patient_One.xml

相关问题