如何使用Selenium Webdriver上传文件? [英] How to upload file using Selenium Webdriver?

查看:196
本文介绍了如何使用Selenium Webdriver上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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



如果我点击浏览按钮弹出显示。



文件在E盘里面。尝试了下面的脚本,但问题仍然是一样的。

  cd.findElement(By.xpath(// * @ ID = 'import_file']))点击(); 
cd.switchTo()。activeElement()
.sendKeys(E:// iMedicor - Karthik / 2.Demographics / Patients_Data / Patient_One.xml);


解决方案

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


$ b

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 code>


Tried lot of suggestions available on stackoverflow regarding upload file using selenium webdriver, but issue still exists.

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. 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

这篇关于如何使用Selenium Webdriver上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆