webdriver 上传文件

欢迎光临娇娇家的美衣阁 http://shop105984718.taobao.com/

利用webdriver上传文件非常方便

 /*上传文件
  * element,上传按钮 例  ("//input[@type='file']")
  * filePath,文件路径,例 C:\testContent.pdf
 */
 public void fileUpload(String element, String filePath){
  driver.findElement(By.xpath(element)).sendKeys(filePath); 
 }

注意文件路径最后应当能解析为绝对路径,可先获取当前工程所在路径再拼接,如下所示

  String curPath = new File("").getAbsolutePath();   //所得结果为D:workspacese2integratedapp
  String filePath = curPath + "\src\test\java\data\page\logo.jpg";

如果想要使用编译后的路径,则可使用如下方法

  String curPath = GetPath.class.getResource("/").toString();

所得结果为file:/D:/workspace/se2/integratedapp/target/test-classes/

欢迎光临娇娇家的美衣阁 http://shop105984718.taobao.com/

原文地址:https://www.cnblogs.com/joewu/p/3701121.html