有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用SeleniumWebDriver上传文件?

在stackoverflow上尝试了很多关于使用SeleniumWebDriver上传文件的建议,但问题仍然存在

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

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) 个答案

  1. # 1 楼答案

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

    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