简体   繁体   English

如何使用Selenium Webdriver上传文件?

[英]How to upload file using Selenium Webdriver?

Tried lot of suggestions available on stackoverflow regarding upload file using selenium webdriver, but issue still exists. 尝试了许多关于使用硒Webdriver上传文件的关于stackoverflow的建议,但是问题仍然存在。

User clicks on Browse button, then a new popup window is displayed and then script will try to upload the file. 用户单击“浏览”按钮,然后显示一个新的弹出窗口,然后脚本将尝试上载该文件。 My code below: 我的代码如下:

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

在此处输入图片说明

If I clicked on Browse button following popup is displayed. 如果单击“浏览”按钮,则会显示以下弹出窗口。 在此处输入图片说明

File is inside E drive. 文件位于E驱动器中。 Tried script like below as well, but issue is still same. 也尝试过以下脚本,但问题仍然相同。

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

Below code solved the issue.. 下面的代码解决了这个问题。

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

Actually the filepath caused a issue for me.. I have used 实际上,文件路径对我造成了问题。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM